~michalr/blog2gmi

744daf5636ba41dc0ce991f5215fdcb1c9be69b7 — Michał Rudowicz 2 years ago b783509
Added posts path argument, getting post publication date from filename
3 files changed, 42 insertions(+), 4 deletions(-)

M shard.lock
M shard.yml
M src/main.cr
M shard.lock => shard.lock +4 -0
@@ 1,5 1,9 @@
version: 2.0
shards:
  crustache:
    git: https://github.com/MakeNowJust/crustache.git
    version: 2.4.3

  map_parallel:
    git: https://git.sr.ht/~michalr/map_parallel
    version: 0.0.1+git.commit.a7c0be201c6695fa24b5e4f5fdffa3c707a54eb1

M shard.yml => shard.yml +2 -0
@@ 12,6 12,8 @@ dependencies:
    github: icyleaf/markd
  map_parallel:
    git: https://git.sr.ht/~michalr/map_parallel
  crustache:
    github: MakeNowJust/crustache

license: GPLv3


M src/main.cr => src/main.cr +36 -4
@@ 2,17 2,49 @@ require "markd"
require "./gmi_renderer"
require "./extract_jekyll_front_matter"
require "map_parallel"
require "option_parser"

Dir.new(".").entries
posts_dir = "."

OptionParser.parse do |parser|
  parser.banner = "Usage: blog2gmi [arguments]"
  parser.on("-p PATH", "--posts-dir=PATH", "Path to directory with Jekyll posts") { |dir| posts_dir = dir }
  parser.on("-h", "--help", "Show this help") do
    puts parser
    exit
  end
  parser.invalid_option do |flag|
    STDERR.puts "ERROR: #{flag} is not a valid option."
    STDERR.puts parser
    exit(1)
  end
end

def g(matched, field)
  a = matched.try &.[field]
  return a.to_i if a.is_a?(String)
  return 0
end

Dir.new(posts_dir).entries
  .select! { |x| /\.(md|markdown)$/ =~ x }
  .map_parallel do |fname|
    entry = File.read(fname)
    entry = File.read(Path.new(posts_dir, fname))
    extract = extract_jekyll_front_matter(entry)
    gmi_document, warnings = GmiRenderer.new.render(Markd::Parser.parse(extract[:doc]))
    warnings.each { |w| puts "#{fname}:#{w}" }
    {fname, extract[:front_matter], gmi_document}
  end
  .each do |fname, front_matter, rendered|
    puts "======= #{fname} : #{front_matter["title"]?} =======\n"
  .map do |fname, front_matter, rendered|
    matched_publication_date = /^(?<year>\d+)-(?<month>\d+)-(?<day>\d+)-.*/.match(fname)
    article_date = Time.utc(
      g(matched_publication_date, "year"),
      g(matched_publication_date, "month"),
      g(matched_publication_date, "day"))
    {fname, front_matter, article_date, rendered}
  end
  .each do |fname, front_matter, article_date, rendered|
    puts "======= #{fname} (#{article_date}) : #{front_matter["title"]?} =======\n"
    puts "#{front_matter}\n"
    puts "#{rendered}\n"
  end