Module: MaRuKu::Out::Ansi

Included in:
MDElement
Defined in:
lib/amp-front/third_party/maruku.rb,
lib/amp-front/third_party/maruku/output/to_ansi.rb

Overview

Functions for exporting to ANSI

Instance Method Summary collapse

Instance Method Details

#array_to_ansi(array, join_char = '') ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 197

def array_to_ansi(array, join_char='')
  e = []
  array.each do |c|
    method = c.kind_of?(MDElement) ? "to_ansi_#{c.node_type}" : "to_ansi"

    if not c.respond_to?(method)
      next
    end

    h =  c.send(method)

    if h.nil?
      raise "Nil ansi for #{c.inspect} created with method #{method}"
    end

    if h.kind_of?Array
      e = e + h
    else
      e << h
    end
  end

  e.join(join_char)
end

#children_to_ansiObject

Convert each child to html



193
194
195
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 193

def children_to_ansi
  array_to_ansi(@children)
end

#latex_color(s, command = 'color') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 69

def latex_color(s, command='color')
  if s =~ /^\#(\w\w)(\w\w)(\w\w)$/
    r = $1.hex; g = $2.hex; b=$3.hex
    # convert from 0-255 to 0.0-1.0
    r = r / 255.0; g = g / 255.0; b = b / 255.0; 
    "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r,g,b]
  elsif s =~ /^\#(\w)(\w)(\w)$/
    r = $1.hex; g = $2.hex; b=$3.hex
    # convert from 0-15 to 0.0-1.0
    r = r / 15.0; g = g / 15.0; b = b / 15.0; 
    "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r,g,b]
  else 
    "\\#{command}{#{s}}"
  end
end

#to_ansi_abbrObject



188
189
190
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 188

def to_ansi_abbr
  children_to_ansi
end

#to_ansi_codeObject



85
86
87
88
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 85

def to_ansi_code
  source = self.raw_code
  return source.to_s.black.on_green+"\n\n"
end

#to_ansi_email_addressObject



179
180
181
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 179

def to_ansi_email_address
  "#{self.email}"
end

#to_ansi_emphasisObject



122
123
124
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 122

def to_ansi_emphasis
  "#{children_to_ansi}".underline
end

#to_ansi_headerObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 91

def to_ansi_header
   level = self.level
  title = children_to_ansi
   length = title.size
   
   if level == 1 || level == 3 then title = title.bold end
   if level == 1 || level == 2 then title = title.underline end
   
   %{#{title}\n\n}
end

#to_ansi_hruleObject



58
59
60
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 58

def to_ansi_hrule
  "\n#{'-' * 40}\n" 
end


148
149
150
151
152
153
154
155
156
157
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 148

def to_ansi_im_link
  url = self.url

  if url[0,1] == '#'
    url = url[1,url.size]
    return "#{children_to_ansi} (#{url})"
  else
    return "#{children_to_ansi} (#{url})"
  end
end


135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 135

def to_ansi_immediate_link
  url = self.url
  text = url.gsub(/^mailto:/,'') # don't show mailto
   #  gsub('~','$\sim$')
  text = latex_escape(text)
  if url[0,1] == '#'
    url = url[1,url.size]
    return "\\hyperlink{#{url}}{#{text}}"
  else
    return "\\href{#{url}}{#{text}}"
  end
end

#to_ansi_inline_codeObject



130
131
132
133
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 130

def to_ansi_inline_code
  source = self.raw_code
  return source.to_s.black.on_green
end

#to_ansi_liObject



112
113
114
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 112

def to_ansi_li
  "* #{children_to_ansi}\n"  
end

#to_ansi_li_spanObject



115
116
117
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 115

def to_ansi_li_span
  "* #{children_to_ansi}\n"
end

#to_ansi_linebreakObject



61
62
63
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 61

def to_ansi_linebreak
  "\n " 
end


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 159

def to_ansi_link
  id = self.ref_id
  ref = @doc.refs[id]
  if not ref
    $stderr.puts "Could not find id = '#{id}'"
    return children_to_ansi
  else
    url = ref[:url]
    #title = ref[:title] || 'no title'

    if url[0,1] == '#'
      url = url[1,url.size]
      return "\\hyperlink{#{url}}{#{children_to_ansi}}"
    else
      return "\\href{#{url}}{#{children_to_ansi}}"
    end
  end

end

#to_ansi_olObject



109
110
111
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 109

def to_ansi_ol
  wrap_as_environment('enumerate')
end

#to_ansi_paragraphObject



65
66
67
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 65

def to_ansi_paragraph 
  children_to_ansi+"\n\n"
end

#to_ansi_quoteObject



106
107
108
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 106

def to_ansi_quote
  wrap_as_environment('quote')
end

#to_ansi_raw_htmlObject



183
184
185
186
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 183

def to_ansi_raw_html
  #'{\bf Raw HTML removed in ansi version }'
  ""
end

#to_ansi_strongObject



119
120
121
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 119

def to_ansi_strong
"#{children_to_ansi}".bold
end

#to_ansi_ulObject



102
103
104
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 102

def to_ansi_ul
  children_to_ansi + "\n"
end

#wrap_as_span(c) ⇒ Object



126
127
128
# File 'lib/amp-front/third_party/maruku/output/to_ansi.rb', line 126

def wrap_as_span(c)
  "{#{c} #{children_to_ansi}}"
end