repubmark/exe/repubmark
2024-09-28 03:10:33 +04:00

46 lines
1.2 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
lib = File.expand_path('../lib', __dir__).freeze
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
require 'bundler/setup'
require 'i18n'
require 'repubmark'
I18n.load_path = Dir[
Pathname.new(__dir__).join('../vendor/rails-i18n/rails/locale/**/*.yml')
].each(&:freeze).freeze
config_filename = String(ARGV[0]).freeze
profile = String(ARGV[1]).freeze
config = Repubmark::Config.load_file config_filename, profile
$article = Repubmark::Elems::Article.new config
template = $stdin.read.freeze
context = BasicObject.new
def context.article = $article
context.instance_eval template, __FILE__, __LINE__
def wrap(str)
words = str.strip.split
lines = [[]]
words.each do |word|
lines << [] if [*lines.last, word].join(' ').length > 70
lines.last << word
end
lines.map { |line| "#{line.join(' ')}\n" }.join.freeze
end
output =
case config.format
when :chapters then JSON.pretty_generate $article.chapters
when :gemtext then $article.to_gemtext
when :html then $article.to_html
when :summary_plain then wrap $article.to_summary_plain
when :word_count then $article.word_count
end.to_s.strip
puts output unless output.empty?