Class: Pandoc2ReVIEW

Inherits:
Object
  • Object
show all
Defined in:
lib/pandoc2review.rb

Instance Method Summary collapse

Instance Method Details

#mainObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pandoc2review.rb', line 8

def main
  luadir = (Pathname.new(__FILE__).realpath.dirname + '../lua').realpath
  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')]

    if file.match?(/\.md$/i)
      args += ['-f', 'markdown-auto_identifiers-smart+east_asian_line_breaks']

      if @disableeaw
        args += ['-M', 'softbreak:true']
      end

      if @hideraw
        args += ['-M', 'hideraw:true']
      end
    end

    if @stripemptydev
      args += ['-M', 'stripemptydev:true']
    end

    if @classicwriter
      args += ['-M', 'classicwriter:true']
    end

    if @heading
      args += ["--shift-heading-level-by=#{@heading}"]
    end

    args.push(file)

    stdout, stderr, status = Open3.capture3(*args)
    unless status.success?
      $stderr.puts stderr
      exit 1
    end
    print modify_result(stdout)
  end
end

#modify_result(s) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pandoc2review.rb', line 96

def modify_result(s)
  s.gsub('<P2RBR/>') 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

    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

    space
  end
end

#parse_argsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pandoc2review.rb', line 54

def parse_args
  @heading = nil
  @disableeaw = nil
  @hideraw = nil
  @stripemptydev = nil
  @classicwriter = nil
  opts = OptionParser.new
  opts.banner = 'Usage: pandoc2review [option] file [file ...]'
  opts.version = '2.0'

  opts.on('--help', 'Prints this message and quit.') do
    puts opts.help
    exit 0
  end
  opts.on('--shiftheading num', 'Add <num> 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.on('--strip-emptydev', 'Strip <div> without any id or class') do
    @stripemptydev = true
  end
  opts.on('--classic-writer', 'Prefer classic custom writer on Pandoc 3.x to be compatible with Pandoc 2.x') do
    @classicwriter = true
  end

  begin
    opts.parse!(ARGV)
  rescue OptionParser::ParseError
    puts opts.help
    exit 1
  end
  if ARGV.size != 1
    puts opts.help
    exit 0
  end
end