Created
March 8, 2019 20:58
-
-
Save evantravers/f52133b9530ce006fd30e4107068de20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule GetLinks do | |
@moduledoc false | |
# TODO: Get this to be able to take an argument | |
def is_current_month?(file) do | |
{{file_year, file_month, _day}, _time} = | |
file | |
|> File.stat!(time: :local) | |
|> Map.get(:mtime) | |
{{system_year, system_month, _day}, _time} = | |
:calendar.local_time() | |
{file_year, file_month} == {system_year, system_month} | |
end | |
def print_reference(file) do | |
file | |
|> File.read! | |
|> extract_title | |
|> extract_content | |
|> extract_link | |
|> build_markdown | |
end | |
def extract_title(string) do | |
title = | |
string | |
|> String.split("\n") | |
|> hd | |
%{title: title, source: string} | |
end | |
def extract_content(obj = %{source: string}) do | |
content = | |
string | |
|> String.split("\n", trim: true) | |
|> Enum.slice(1..-2) | |
|> Enum.join("\n\n") | |
Map.put(obj, :content, content) | |
end | |
def extract_link(obj) do | |
link = | |
~r/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/ | |
|> Regex.run(obj.source) | |
|> hd | |
obj | |
|> Map.put(:link, link) | |
end | |
def build_markdown(obj) do | |
""" | |
## [#{obj.title}](#{obj.link}) | |
#{obj.content} | |
[Read more...](#{obj.link}) | |
""" | |
end | |
def run do | |
with :ok <- File.cd(Path.expand("~/dropbox/wiki/links")) do | |
File.ls!() | |
|> Enum.filter(& &1 =~ ~r/.txt/) | |
|> Enum.filter(&is_current_month?/1) | |
end | |
|> Enum.sort_by(&File.stat!(&1, time: :local)) | |
|> Enum.map(&print_reference/1) | |
|> Enum.join | |
|> IO.puts | |
end | |
end | |
GetLinks.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment