Class: MarkdownToTeX::TextProcessor
- Inherits:
-
Object
- Object
- MarkdownToTeX::TextProcessor
- 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
-
.bib_name_map(n) ⇒ Object
Citation Handling.
- .comment_line(s) ⇒ Object
- .label_name_map(n) ⇒ Object
- .label_split(s) ⇒ Object
- .level_tag(d) ⇒ Object
-
.process_final(text, macros) ⇒ Object
Process on whole output text.
-
.process_header(text, level) ⇒ Object
Process a header.
-
.process_paragraph(text) ⇒ Object
Process a paragraph.
- .ref_name_map(n) ⇒ Object
- .ref_name_map_cite(k) ⇒ Object
- .reference(text) ⇒ Object
-
.short_desc(s) ⇒ Object
map short|long.
- .tag_name_map(n) ⇒ Object
Class Method Details
.bib_name_map(n) ⇒ Object
Citation Handling
34 35 36 |
# File 'lib/markdown_to_tex/text_processor.rb', line 34 def self.bib_name_map(n) @@cite_map.has_key?(n) ? @@cite_map[n] : n end |
.comment_line(s) ⇒ Object
126 127 128 129 130 |
# File 'lib/markdown_to_tex/text_processor.rb', line 126 def self.comment_line(s) px = (78 - 1 - s.length) px = 1 if px < 1 "%" * px + " " + s + "\n" end |
.label_name_map(n) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/markdown_to_tex/text_processor.rb', line 46 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
81 82 83 84 85 86 87 88 |
# File 'lib/markdown_to_tex/text_processor.rb', line 81 def self.label_split(s) label = "" if s =~ /\s*\[([^\]]+)\]/ label = $1 s = s.sub(/\s*\[([^\]]+)\]/, '') end [s, label] end |
.level_tag(d) ⇒ Object
99 100 101 |
# File 'lib/markdown_to_tex/text_processor.rb', line 99 def self.level_tag(d) @@level_tag[d + @@level_shift] end |
.process_final(text, macros) ⇒ Object
Process on whole output text
120 121 122 123 124 |
# File 'lib/markdown_to_tex/text_processor.rb', line 120 def self.process_final(text, macros) # Macro expansions macros.each {|m, v| text = text.gsub(m, v) } text end |
.process_header(text, level) ⇒ Object
Process a header
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/markdown_to_tex/text_processor.rb', line 104 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
27 28 29 30 |
# File 'lib/markdown_to_tex/text_processor.rb', line 27 def self.process_paragraph(text) text = text.gsub(/\(\((.*)\)\)/, '\\ITEM{\1}') # description format self.reference(text) end |
.ref_name_map(n) ⇒ Object
59 60 61 |
# File 'lib/markdown_to_tex/text_processor.rb', line 59 def self.ref_name_map(n) self.label_name_map(n) end |
.ref_name_map_cite(k) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/markdown_to_tex/text_processor.rb', line 63 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
74 75 76 77 78 79 |
# File 'lib/markdown_to_tex/text_processor.rb', line 74 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
90 91 92 93 94 95 96 97 |
# File 'lib/markdown_to_tex/text_processor.rb', line 90 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
38 39 40 41 42 43 44 |
# File 'lib/markdown_to_tex/text_processor.rb', line 38 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 |