diff --git a/exe/pandoc2review b/exe/pandoc2review index 99bc7a4..031583c 100755 --- a/exe/pandoc2review +++ b/exe/pandoc2review @@ -1,5 +1,12 @@ #!/usr/bin/env ruby # -*- coding: utf-8 -*- -# Copyright 2020 Kenshi Muto +# Copyright 2020-2021 Kenshi Muto + +require 'pathname' +bindir = Pathname.new(__FILE__).realpath.dirname +$LOAD_PATH.unshift((bindir + '../lib').realpath) + require 'pandoc2review' -main + +p2r = Pandoc2ReVIEW.new +p2r.main diff --git a/lib/pandoc2review.rb b/lib/pandoc2review.rb index 3a63aec..d362596 100644 --- a/lib/pandoc2review.rb +++ b/lib/pandoc2review.rb @@ -1,98 +1,99 @@ # -*- coding: utf-8 -*- -# Copyright 2020 Kenshi Muto +# Copyright 2020-2021 Kenshi Muto require 'optparse' require 'unicode/eaw' require 'pathname' require 'open3' -def main - luadir = Pathname.new(__FILE__).realpath.dirname +class Pandoc2ReVIEW + def main + luadir = ((Pathname.new(__FILE__)).realpath.dirname + '../lua').realpath + parse_args - parse_args + ARGV.each do |file| + unless File.exist?(file) + puts "#{file} not exist. skip." + next + end + args = ['pandoc', '-t', File.join(luadir, 'review.lua'), '--lua-filter', File.join(luadir, 'filters.lua')] - ARGV.each do |file| - unless File.exist?(file) - puts "#{file} not exist. skip." - next - end - args = ['pandoc', '-t', File.join(luadir, 'review.lua'), '--lua-filter', File.join(luadir, 'filters.lua')] + if file =~ /\.md$/i + args += ['-f', 'markdown-auto_identifiers-smart+east_asian_line_breaks'] - if file =~ /\.md$/i - args += ['-f', 'markdown-auto_identifiers-smart+east_asian_line_breaks'] + if @disableeaw + args += ['-M', "softbreak:true"] + end - if @disableeaw - args += ['-M', "softbreak:true"] + if @hideraw + args += ['-M', "hideraw:true"] + end end - if @hideraw - args += ['-M', "hideraw:true"] + if @heading + args += ["--shift-heading-level-by=#{@heading}"] end - end - if @heading - args += ["--shift-heading-level-by=#{@heading}"] - end - - args.push(file) + args.push(file) - stdout, stderr, status = Open3.capture3(*args) - unless status.success? - STDERR.puts stderr - exit 1 + stdout, stderr, status = Open3.capture3(*args) + unless status.success? + STDERR.puts stderr + exit 1 + end + print modify_result(stdout) end - print modify_result(stdout) end -end - -def parse_args - @heading = nil - @disableeaw = nil - @hideraw = nil - opts = OptionParser.new - opts.banner = 'Usage: pandoc2review [option] file [file ...]' - opts.version = '1.0' - opts.on('--help', 'Prints this message and quit.') do - puts opts.help - exit 0 - end - opts.on('--shiftheading num', 'Add to heading level.') do |v| - @heading = v - end - opts.on('--disable-eaw', "Disable compositing a paragraph with Ruby's EAW library.") do - @disableeaw = true - end - opts.on('--hideraw', "Hide raw inline/block with no review format specified.") do - @hideraw = true - end + def parse_args + @heading = nil + @disableeaw = nil + @hideraw = nil + opts = OptionParser.new + opts.banner = 'Usage: pandoc2review [option] file [file ...]' + opts.version = '1.2' + + opts.on('--help', 'Prints this message and quit.') do + puts opts.help + exit 0 + end + opts.on('--shiftheading num', 'Add to heading level.') do |v| + @heading = v + end + opts.on('--disable-eaw', "Disable compositing a paragraph with Ruby's EAW library.") do + @disableeaw = true + end + opts.on('--hideraw', "Hide raw inline/block with no review format specified.") do + @hideraw = true + end - opts.parse!(ARGV) - if ARGV.size != 1 - puts opts.help - exit 0 + opts.parse!(ARGV) + if ARGV.size != 1 + puts opts.help + exit 0 + end end -end -def modify_result(s) - s.gsub('') do - tail = $`[-1] - head = $'[0] - return '' if tail.nil? || head.nil? + def modify_result(s) + s.gsub('') do + tail = $`[-1] + head = $'[0] + return '' if tail.nil? || head.nil? + + space = ' ' + if %i[F W H].include?(Unicode::Eaw.property(tail)) && + %i[F W H].include?(Unicode::Eaw.property(head)) && + tail !~ /\p{Hangul}/ && head !~ /\p{Hangul}/ + space = '' + end - space = ' ' - if %i[F W H].include?(Unicode::Eaw.property(tail)) && - %i[F W H].include?(Unicode::Eaw.property(head)) && - tail !~ /\p{Hangul}/ && head !~ /\p{Hangul}/ - space = '' - end + if (%i[F W H].include?(Unicode::Eaw.property(tail)) && + tail !~ /\p{Hangul}/) || + (%i[F W H].include?(Unicode::Eaw.property(head)) && + head !~ /\p{Hangul}/) + space = '' + end - if (%i[F W H].include?(Unicode::Eaw.property(tail)) && - tail !~ /\p{Hangul}/) || - (%i[F W H].include?(Unicode::Eaw.property(head)) && - head !~ /\p{Hangul}/) - space = '' + space end - - space end end diff --git a/lib/filters.lua b/lua/filters.lua similarity index 100% rename from lib/filters.lua rename to lua/filters.lua diff --git a/lib/review.lua b/lua/review.lua similarity index 100% rename from lib/review.lua rename to lua/review.lua diff --git a/pandoc2review.gemspec b/pandoc2review.gemspec index e3b9ba8..e125940 100644 --- a/pandoc2review.gemspec +++ b/pandoc2review.gemspec @@ -3,7 +3,7 @@ Gem::Specification.new do |spec| # spec.version = Pandoc2review::VERSION spec.version = "1.1.0" spec.authors = ["kmuto"] - spec.email = ["kmuto@debian.org"] + spec.email = ["kmuto@kmuto.jp"] spec.summary = %q{Re:VIEW Filter/Writer for Pandoc.} spec.description = %q{Re:VIEW Filter/Writer for Pandoc.} @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] + spec.require_paths = ["lib", "lua"] spec.add_dependency('unicode-eaw') spec.add_development_dependency('simplecov') diff --git a/test/test_helper.rb b/test/test_helper.rb index 67299a5..5dbd598 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,15 +3,16 @@ require 'open3' def pandoc(src, opts: nil, err: nil) - args = 'pandoc -t lib/review.lua --lua-filter=lib/filters.lua -f markdown-auto_identifiers-smart+east_asian_line_breaks' + args = 'pandoc -t lua/review.lua --lua-filter=lua/filters.lua -f markdown-auto_identifiers-smart+east_asian_line_breaks' + p2r = Pandoc2ReVIEW.new if opts args += ' ' + opts end if err stdout, stderr, status = Open3.capture3(args, stdin_data: src) - return modify_result(stdout), stderr + [p2r.modify_result(stdout), stderr] else stdout, status = Open3.capture2(args, stdin_data: src) - modify_result(stdout) + p2r.modify_result(stdout) end end