Class: MarkdownToTeX::TextProcessor

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

Constant Summary collapse

@@tag_namespace =
nil
@@cite_map =
{}
@@level_tag =
["chapter", "section", "subsection", "subsubsection"]
@@level_shift =
0

Class Method Summary collapse

Class Method Details

.bib_name_map(n) ⇒ Object

Citation Handling



33
34
35
# File 'lib/markdown_to_tex/text_processor.rb', line 33

def self.bib_name_map(n)
  @@cite_map.has_key?(n) ? @@cite_map[n] : n
end

.comment_line(s) ⇒ Object



124
125
126
127
128
# File 'lib/markdown_to_tex/text_processor.rb', line 124

def self.comment_line(s)
  px = (78 - 1 - s.length)
  px = 1 if px < 1
  "%" * px + " " + s + "\n"
end

.label_name_map(n) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/markdown_to_tex/text_processor.rb', line 45

def self.label_name_map(n)
  if @@tag_namespace == nil
    return bib_name_map(n)
  end

  if n =~ /(chap|part|sec|subsec|subsubsec|fig|table|eq):(.+)/
    r = $1 + ":" + self.tag_name_map($2)
  else
    r = self.bib_name_map(n)
  end
  r
end

.label_split(s) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/markdown_to_tex/text_processor.rb', line 80

def self.label_split(s)
  label = ""
  if s.sub!(/\s*\[([^\]]+)\]/, '')
    label = $1
  end
  [s, label]
end

.level_tag(d) ⇒ Object



97
98
99
# File 'lib/markdown_to_tex/text_processor.rb', line 97

def self.level_tag(d)
  @@level_tag[d + @@level_shift]
end

.process_final(text, macros) ⇒ Object

Process on whole output text



118
119
120
121
122
# File 'lib/markdown_to_tex/text_processor.rb', line 118

def self.process_final(text, macros)
  # Macro expansions
  macros.each {|m, v| text.gsub!(m, v) }
  text
end

.process_header(text, level) ⇒ Object

Process a header



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/markdown_to_tex/text_processor.rb', line 102

def self.process_header(text, level)
  name, label = self.label_split(text)
  name, name_short = self.short_desc(name)
  tag = self.level_tag(level)
  the_label = self.label_name_map(label)
  star = "" # star ? "*" : ""
  s = self.comment_line(tag.upcase + ": #{name}")
  s << "\\#{tag}#{star}#{name_short}{#{name}}"
  s << "\\label{#{the_label}}" unless label.empty?
  s << "\n"
  s
end

.process_paragraph(text) ⇒ Object

Process a paragraph



26
27
28
29
# File 'lib/markdown_to_tex/text_processor.rb', line 26

def self.process_paragraph(text)
  text.gsub!(/\(\((.*)\)\)/, '\\ITEM{\1}') # description format
  self.reference(text)
end

.ref_name_map(n) ⇒ Object



58
59
60
# File 'lib/markdown_to_tex/text_processor.rb', line 58

def self.ref_name_map(n)
  self.label_name_map(n)
end

.ref_name_map_cite(k) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/markdown_to_tex/text_processor.rb', line 62

def self.ref_name_map_cite(k)
  cite = "CITE"
  text = k.split(/\s*,\s*/).each.map {|kk| self.ref_name_map(kk)}.join(",")
  if text =~ /"(.*)"/
    r = "[#{$1}]"
  else
    r = "\\#{cite}{#{text}}"
  end
  r
end

.reference(text) ⇒ Object



73
74
75
76
77
78
# File 'lib/markdown_to_tex/text_processor.rb', line 73

def self.reference(text)
  text.
       gsub(/\[(this|prev|next):(chap|part|sec|subsec|subsubsec|fig|table)\]/) { "\\#{$1}#{$2}KEY{}" }.
       gsub(/\[((chap|part|sec|subsec|subsubsec|fig|table):[^\]]+)\]/) { "\\#{$2}REF{#{self.ref_name_map($1)}}" }.
       gsub(/\[([^\]]+)\]/) { self.ref_name_map_cite($1);}
end

.short_desc(s) ⇒ Object

map short|long



88
89
90
91
92
93
94
95
# File 'lib/markdown_to_tex/text_processor.rb', line 88

def self.short_desc(s)  # map short|long
  s_short = ""
  if s =~ /\s*([^\|]+)\s*\|\s*(.+)\s*/
   s_short = "[#{$1}]"
    s = $2
  end
  [s, s_short]
end

.tag_name_map(n) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/markdown_to_tex/text_processor.rb', line 37

def self.tag_name_map(n)
  return "" if n == nil
  if @@tag_namespace == nil or n =~ /#{@@tag_namespace}:/
    return n
  end
  @@tag_namespace + ":" + n
end