Module: Opmac2html::MacroParser
- Included in:
- Converter
- Defined in:
- lib/opmac2html/macro_parser.rb
Overview
Mixin providing parsing of macro calls
Constant Summary collapse
- TITLES =
%w(\\tit \\chap \\sec\ \\secc)
- IN_PAR_MACROS =
%w(\\TeX \\LaTeX \\csplain \\url \\ulink)
Instance Method Summary collapse
- #build_table(tb) ⇒ Object
- #parse_image ⇒ Object
- #parse_label ⇒ Object
- #parse_list ⇒ Object
- #parse_list_items ⇒ Object
- #parse_macro ⇒ Object
- #parse_other ⇒ Object
- #parse_table ⇒ Object
- #parse_table_caption(line, tb) ⇒ Object
- #parse_table_cells ⇒ Object
- #parse_title(index) ⇒ Object
- #parse_ttchar ⇒ Object
- #parse_verbatim ⇒ Object
- #process_list_item(line, builder) ⇒ Object
- #title_level(index) ⇒ Object
- #verbinput ⇒ Object
- #verbinput_range ⇒ Object
Instance Method Details
#build_table(tb) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/opmac2html/macro_parser.rb', line 144 def build_table(tb) parse_table_cells.each do |row| tb.add_row(row.map do |cell| if cell.start_with? '\\multispan' cellpart = cell.partition(/\d+/) cellpart[0] + cellpart[1] + parse_par_macros(cellpart[2]) else parse_par_macros cell end end) end end |
#parse_image ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/opmac2html/macro_parser.rb', line 157 def parse_image img = cut_at_matching(@input, '\\inspic ', "\n")[0] .partition(/ [\n\}]/)[0] part = cut_at("\n\n") if part.include? '\\label' @builder.add_anchor(cut_at_matching(part, '\\label[', ']')[0]) end if part.include? '\\caption/f' @builder.add_figure img, cut_at_matching( part, '\\caption/f ', "\n\n")[0] else @builder.add_img img end end |
#parse_label ⇒ Object
172 173 174 175 176 |
# File 'lib/opmac2html/macro_parser.rb', line 172 def parse_label part = cut_at_matching @input, '[', ']' @builder.add_anchor part[0] @input = part[1] end |
#parse_list ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/opmac2html/macro_parser.rb', line 55 def parse_list list = parse_list_items builder = ListBuilder.new(list[0].partition('\style ')[2][0]) list[1..-1].each do |line| process_list_item line, builder end @builder.add_list builder.to_s end |
#parse_list_items ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/opmac2html/macro_parser.rb', line 64 def parse_list_items list, @input = *(cut_at_matching(@input, '\\begitems', '\\enditems')) list.split("\n").reduce([]) do |a, e| if /\*|\\begitems|\\enditems/.match(e) || a.empty? a << e else a[-1].concat "\n" + e a end end end |
#parse_macro ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/opmac2html/macro_parser.rb', line 7 def parse_macro title_index = TITLES.index { |t| @input.start_with? t } if title_index parse_title title_index elsif @input.start_with? '\\begtt' parse_verbatim elsif @input.start_with? '\\verbinput' verbinput elsif @input.start_with? '\\begitems' parse_list elsif @input.start_with? '\\activettchar' parse_ttchar elsif IN_PAR_MACROS.any? { |m| @input.start_with? m } parse_par else parse_other end end |
#parse_other ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/opmac2html/macro_parser.rb', line 92 def parse_other part_line = @input.partition("\n") if %w(\\table \\caption/t).any? { |s| part_line[0].include? s } parse_table elsif part_line[0].include?('\\inspic') parse_image elsif part_line[0].include?('\\def') part = cut_at_match_with_start(@input, '{', '}') err part[0] + part[1] @input = part[2] elsif part_line[0].start_with?('\\noindent') err cut_at(/\s/) elsif part_line[0].start_with?('\\nonum') err cut_at(/\s/) elsif part_line[0].start_with? '\\label' parse_label elsif part_line[0].start_with? '\\centerline' text = cut_at_matching(part_line[0], '{', '}')[0] @builder.add_par parse_par_macros text @input = part_line[2] else err part_line[0] @input = part_line[2] end end |
#parse_table ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/opmac2html/macro_parser.rb', line 118 def parse_table tb = TableBuilder.new (@input.partition("\n")[0], tb) build_table tb (cut_at("\n\n"), tb) @builder.add_table tb.to_s end |
#parse_table_caption(line, tb) ⇒ Object
129 130 131 132 133 |
# File 'lib/opmac2html/macro_parser.rb', line 129 def (line, tb) return unless line.include? '\\caption/t' = line.partition('\\caption/t ')[2].partition("\n")[0] tb.(parse_par_macros()) end |
#parse_table_cells ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/opmac2html/macro_parser.rb', line 135 def parse_table_cells text = @input.partition(/\\table\s*\{[^\{]*/)[2] part = cut_at_matching(text, '{', '}') @input = part[1] part[0].split(/\\cr.*/).map { |r| r.split(/\s&\s/).map(&:strip) } .reject(&:empty?) end |
#parse_title(index) ⇒ Object
26 27 28 29 30 |
# File 'lib/opmac2html/macro_parser.rb', line 26 def parse_title(index) @min_index ||= index title_text = @preproc.process_text(cut_at("\n\n").partition(' ')[2]) @builder.add_title([title_level(index), title_text]) end |
#parse_ttchar ⇒ Object
86 87 88 89 90 |
# File 'lib/opmac2html/macro_parser.rb', line 86 def parse_ttchar cut_at 'r' @preproc.ttchar = @ttchar = @input[0] @input = @input[1..-1] end |
#parse_verbatim ⇒ Object
36 37 38 39 |
# File 'lib/opmac2html/macro_parser.rb', line 36 def parse_verbatim cut_at "\n" @builder.add_verbatim cut_at '\\endtt' end |
#process_list_item(line, builder) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/opmac2html/macro_parser.rb', line 76 def process_list_item(line, builder) if line.include? '\\begitems' builder.begitems line.partition('\style ')[2][0] elsif line.include? '\\enditems' builder.enditems else builder.add_item parse_par_macros(line.partition(/\*\s/)[2]) end end |
#title_level(index) ⇒ Object
32 33 34 |
# File 'lib/opmac2html/macro_parser.rb', line 32 def title_level(index) index + 1 - @min_index end |
#verbinput ⇒ Object
41 42 43 44 45 |
# File 'lib/opmac2html/macro_parser.rb', line 41 def verbinput beg_line, end_line = *verbinput_range file = File.open(cut_at("\n").strip, 'r') { |input| input.readlines } @builder.add_verbatim(file[beg_line - 1..end_line - 1].join) end |
#verbinput_range ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/opmac2html/macro_parser.rb', line 47 def verbinput_range cut_at '(' beg_line = cut_at('-').to_i end_line = cut_at(')').to_i end_line = -1 if end_line == 0 [beg_line, end_line] end |