module Opmac2html::MacroParser

Mixin providing parsing of macro calls

Constants

IN_PAR_MACROS
TITLES

Public Instance Methods

build_table(tb) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/opmac2html/macro_parser.rb, line 118
def parse_table
  tb = TableBuilder.new
  parse_table_caption(@input.partition("\n")[0], tb)

  build_table tb

  parse_table_caption(cut_at("\n\n"), tb)

  @builder.add_table tb.to_s
end
parse_table_caption(line, tb) click to toggle source
# File lib/opmac2html/macro_parser.rb, line 129
def parse_table_caption(line, tb)
  return unless line.include? '\\caption/t'
  caption = line.partition('\\caption/t ')[2].partition("\n")[0]
  tb.add_caption(parse_par_macros(caption))
end
parse_table_cells() click to toggle source
# 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&amp;\s/).map(&:strip) }
    .reject(&:empty?)
end
parse_title(index) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/opmac2html/macro_parser.rb, line 32
def title_level(index)
  index + 1 - @min_index
end
verbinput() click to toggle source
# 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() click to toggle source
# 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