Module: Opmac2html::ParagraphParser

Included in:
Converter
Defined in:
lib/opmac2html/paragraph_parser.rb

Overview

Mixin providing parsing of paragraphs and other text elements

Instance Method Summary collapse

Instance Method Details

#dump_all(text) ⇒ Object



30
31
32
33
# File 'lib/opmac2html/paragraph_parser.rb', line 30

def dump_all(text)
  err text unless text.empty?
  ''
end

#dump_to_space(text) ⇒ Object



35
36
37
38
39
# File 'lib/opmac2html/paragraph_parser.rb', line 35

def dump_to_space(text)
  part = text.partition(/\s/)
  err part[0]
  part[2]
end

#extract_from_braces(text, par_builder) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/opmac2html/paragraph_parser.rb', line 140

def extract_from_braces(text, par_builder)
  if text.index(/\{\\\w/) == 0
    par_special dump_to_space(text), par_builder
  elsif text.start_with? '{'
    par_special cut_at_matching(text, '{', '}')[0], par_builder
  else
    dump_all text
  end
end

#par_macro(text, par_builder) ⇒ Object



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
# File 'lib/opmac2html/paragraph_parser.rb', line 54

def par_macro(text, par_builder)
  if %w(\\url \\ulink \\fnote \\ref \\pgref).any? { |p| text.start_with? p }
    parse_clickable text, par_builder
  elsif text.start_with? '\\begtt'
    par_verbatim text, par_builder
  elsif text.start_with? '\\dots'
    par_builder.add_word '…'
    text.partition('s')[2]
  elsif text.index(/(\\\w+)?\{/) == 0
    part = cut_at_match_with_start(text, '{', '}')
    if %w(\\TeX \\LaTeX \\csplain).any? { |p| text.start_with? p }
      par_builder.add_word(@preproc.process_text(part[0] + part[1]))
    else
      parse_format_block part[0], par_builder
    end
    part[2]
  elsif %w(\\it \\em \\bf \\tt).any? { |p| text.start_with? p }
    parse_format_block text, par_builder
    ''
  elsif text.index(/\\\w*\s/) == 0
    dump_to_space text
  else
    dump_all text
  end
end

#par_special(text, par_builder) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/opmac2html/paragraph_parser.rb', line 19

def par_special(text, par_builder)
  case text[0]
  when @ttchar
    parse_code text, par_builder
  when '$'
    parse_math text, par_builder
  else
    par_macro text, par_builder
  end
end

#par_verbatim(text, par_builder) ⇒ Object



92
93
94
95
96
# File 'lib/opmac2html/paragraph_parser.rb', line 92

def par_verbatim(text, par_builder)
  part = cut_at_matching(text, '\\begtt', '\\endtt')
  par_builder.add_verbatim(part[0])
  part[1]
end

#parse_clickable(text, par_builder) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/opmac2html/paragraph_parser.rb', line 80

def parse_clickable(text, par_builder)
  if %w(\\url \\ulink).any? { |p| text.start_with? p }
    parse_link text, par_builder
  elsif text.start_with? '\\fnote'
    parse_fnote text, par_builder
  elsif %w(\\ref \\pgref).any? { |p| text.start_with? p }
    part = cut_at_matching(text, '[', ']')
    par_builder.add_link("##{part[0]}", part[0])
    part[1]
  end
end

#parse_code(text, par_builder) ⇒ Object



41
42
43
44
45
# File 'lib/opmac2html/paragraph_parser.rb', line 41

def parse_code(text, par_builder)
  part = text[1..-1].partition(@ttchar)
  par_builder.add_code part[0]
  part[2]
end

#parse_fnote(text, par_builder) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/opmac2html/paragraph_parser.rb', line 150

def parse_fnote(text, par_builder)
  part = cut_at_match_with_start(text, '{', '}')
  fnote = parse_par_macros(part[0][part[0].index('{') + 1..-1])
  num = @builder.add_fnote(fnote)
  par_builder.add_link("##{num}", "<sup>#{num}</sup>")
  part[2]
end

#parse_format(text, par_builder, t) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/opmac2html/paragraph_parser.rb', line 127

def parse_format(text, par_builder, t)
  case text[0..3]
  when '\\it ', '\\em ', '{\\it', '{\\em'
    par_builder.add_em t
  when '\\bf ', '{\\bf'
    par_builder.add_strong t
  when '\\tt ', '{\\tt'
    par_builder.add_code t
  else
    extract_from_braces text, par_builder
  end
end

#parse_format_block(text, par_builder) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/opmac2html/paragraph_parser.rb', line 118

def parse_format_block(text, par_builder)
  t = parse_par_macros(text[4..-1]).lstrip if text[4]
  if text.start_with? '\\uv{'
    par_builder.add_quote t
  else
    parse_format text, par_builder, t
  end
end


98
99
100
101
102
103
104
105
106
# File 'lib/opmac2html/paragraph_parser.rb', line 98

def parse_link(text, par_builder)
  part = text.partition('}')
  if text.start_with? '\\url'
    parse_url part[0], par_builder
  else
    parse_ulink part[0], par_builder
  end
  part[2]
end

#parse_math(text, par_builder) ⇒ Object



47
48
49
50
51
52
# File 'lib/opmac2html/paragraph_parser.rb', line 47

def parse_math(text, par_builder)
  separator = text.start_with?('$$') ? '$$' : '$'
  part = text[separator.length..-1].partition(separator)
  par_builder.add_word separator + part[0] + part[1]
  part[2]
end

#parse_parObject



4
5
6
7
# File 'lib/opmac2html/paragraph_parser.rb', line 4

def parse_par
  slice = cut_at_with_sep(/\n\n|\n\\begtt|\n\\.*skip|\\par[\\ ]/)
  @builder.add_par(parse_par_macros(slice.gsub(/^%.*/, '')))
end

#parse_par_macros(text) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/opmac2html/paragraph_parser.rb', line 9

def parse_par_macros(text)
  par_builder = ParBuilder.new
  until text.empty?
    index = text.index(/#{@ttchar}|\\\w|\{|\$/) || text.length
    par_builder.add_word @preproc.process_text text[0...index]
    text = par_special text[index..-1], par_builder
  end
  par_builder.to_s
end


112
113
114
115
116
# File 'lib/opmac2html/paragraph_parser.rb', line 112

def parse_ulink(text, par_builder)
  add = text[text.index('[') + 1...text.index(']')]
  txt = text[text.index('{') + 1..-1]
  par_builder.add_link(add, txt)
end

#parse_url(text, par_builder) ⇒ Object



108
109
110
# File 'lib/opmac2html/paragraph_parser.rb', line 108

def parse_url(text, par_builder)
  par_builder.add_link(text.partition('{')[2])
end