Skip to content

Instantly share code, notes, and snippets.

@leeduckgo
Created April 5, 2024 13:45
Show Gist options
  • Save leeduckgo/92004000434d8078fa0752663b8e409c to your computer and use it in GitHub Desktop.
Save leeduckgo/92004000434d8078fa0752663b8e409c to your computer and use it in GitHub Desktop.
ReadmeAnalyzer
defmodule CodesOnChain.ReadmeAnalyzer do
@moduledoc """
analyze the readme of a repo.
functions:
- analyze_readme(text)
"""
def get_module_doc, do: @moduledoc
def analyze_readme(text) do
ast = Earmark.as_ast!(text)
raw_distribution = get_dis_content(ast, "Distribution")
%{distribution: decode_distribution(raw_distribution)}
end
def decode_distribution(dis) do
dis
|> String.replace(" ", "")
|> String.split("\n")
|> Enum.map(fn elem -> String.split(elem, ",") end)
end
def get_dis_content(ast, header_content) do
index = get_index_of_header(ast, header_content)
{_pre, aft} = Enum.split(ast, index + 1)
aft
|> get_next_pre()
|> get_code_in_pre()
end
def get_index_of_header(ast, header_content) do
ast
|> Enum.find_index(fn el -> ((el |> elem(0)) == "h2" and String.contains?(el |> elem(2) |> Enum.fetch!(0), header_content))
end)
end
def get_next_pre(ast) do
ast
|> Enum.find(fn el -> ((el |> elem(0)) == "pre")
end)
end
def get_code_in_pre(pre) do
{"pre", _, codes, _} = pre
code = codes |> Enum.fetch!(0)
{"code", _, code_content, _} = code
Enum.fetch!(code_content, 0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment