Class: MDElement

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/maruku/to_latex.rb,
lib/maruku/to_html.rb,
lib/maruku/to_latex.rb,
lib/maruku/to_latex.rb,
lib/maruku/parse_span.rb,
lib/maruku/structures.rb,
lib/maruku/to_html.rb

Overview

Some utilities

Direct Known Subclasses

Maruku

Constant Summary collapse

DEFAULT_CODE_COLOR =
'#f0f0e0'
TexHeaders =
{
1=>'section',
2=>'subsection',
3=>'subsubsection',
4=>'paragraph'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMDElement

Returns a new instance of MDElement.



16
17
18
19
20
21
# File 'lib/maruku/structures.rb', line 16

def initialize
	super(); 
	@children = []; 
	@node_type = :unset
	@meta = {};
end

Instance Attribute Details

#childrenObject

Children are either Strings or MDElement



6
7
8
# File 'lib/maruku/structures.rb', line 6

def children
  @children
end

#docObject

reference of containing document (document has list of ref)



14
15
16
# File 'lib/maruku/structures.rb', line 14

def doc
  @doc
end

#metaObject

Hash for metadata contains :id for :link1 :li :want_my_paragraph :header: :level code, inline_code: :raw_code



12
13
14
# File 'lib/maruku/structures.rb', line 12

def meta
  @meta
end

#node_typeObject

Allowed: :document, :paragraph, :ul, :ol, :li, :li_span, :strong, :emphasis, :link1



4
5
6
# File 'lib/maruku/structures.rb', line 4

def node_type
  @node_type
end

Instance Method Details

#array_to_html(array) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/maruku/to_html.rb', line 403

def array_to_html(array)
	e = []
	array.each do |c|
		method = c.kind_of?(MDElement) ? 
		   "to_html_#{c.node_type}" : "to_html"
		
		if not c.respond_to?(method)
			raise "Object does not answer to #{method}: #{c.class} #{c.inspect}"
		end
		
		h =  c.send(method)
		
		if h.nil?
			raise "Nil html for #{c.inspect} created with method #{method}"
		end
		
		if h.kind_of?Array
			e = e + h #h.each do |hh| e << hh end
		else
			e << h
		end
	end
	e
end

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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/maruku/to_latex.rb', line 330

def array_to_latex(array, join_char='')
	e = []
	array.each do |c|
		method = c.kind_of?(MDElement) ? 
		   "to_latex_#{c.node_type}" : "to_latex"
		
		if not c.respond_to?(method)
			raise "Object does not answer to #{method}: #{c.class} #{c.inspect[0,100]}"
		end
		
		h =  c.send(method)
		
		if h.nil?
			raise "Nil html 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_htmlObject

Convert each child to html



399
400
401
# File 'lib/maruku/to_html.rb', line 399

def children_to_html
	array_to_html(@children)
end

#children_to_latexObject

Convert each child to html



326
327
328
# File 'lib/maruku/to_latex.rb', line 326

def children_to_latex
	array_to_latex(@children)
end

#create_html_element(name) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/maruku/to_html.rb', line 141

def create_html_element(name)
	m = Element.new name
		if @meta[:id] then m.attributes['id'] = @meta[:id].to_s end
		if @meta[:style] then m.attributes['style'] = @meta[:style].to_s end
		if @meta[:class] then m.attributes['class'] = @meta[:class].to_s end
	m
end

#define_color_if_necessary(color) ⇒ Object

Returns the name to use and possibly a declaration to append



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/maruku/to_latex.rb', line 58

def define_color_if_necessary(color)
	if color =~ /^\#(\w\w)(\w\w)(\w\w)$/
		# init colors hash in document
		hash = (@doc.meta[:defined_colors] ||= {})
		if hash[color] then 
			return hash[color], nil
		else
			r = $1.hex; g = $2.hex; b=$3.hex
			
			colorname = "maruku_color#{hash.size}"
			
			# convert from 0-255 to 0.0-1.0
			r = r / 255.0 
			g = g / 255.0 
			b = b / 255.0 
			declaration = "\\definecolor{#{colorname}}{rgb}{#{r},#{g},#{b}}\n"
			hash[color] = colorname
			
			return colorname, declaration
		end
	else
		color
	end
end

#each_element(e_node_type, &block) ⇒ Object

yields to each element of specified node_type



292
293
294
295
296
297
298
299
300
301
# File 'lib/maruku/parse_span.rb', line 292

def each_element(e_node_type, &block) 
	@children.each do |c| 
		if c.kind_of? MDElement
			if (not e_node_type) || (e_node_type == c.node_type)
				block.call c
			end
			c.each_element(e_node_type, &block)
		end
	end
end

#get_setting(name, default = nil) ⇒ Object



53
54
55
# File 'lib/maruku/to_latex.rb', line 53

def get_setting(name, default=nil)
	self.meta[name] || @doc.meta[name] || default
end

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

colorname colorMDElement.1,01,0.2,01,0.2,0.3



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/maruku/to_latex.rb', line 85

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]{#{r},#{g},#{b}}"
	else
		"\\#{command}{#{s}}"
	end
end

#latex_escape(source) ⇒ Object

the ultimate escaping



178
179
180
181
182
# File 'lib/maruku/to_latex.rb', line 178

def latex_escape(source)
	s=""; 
	source.each_byte do |b| s+= "\\char%d" % b end
	s
end

#map_match(regexp, &block) ⇒ Object

Try to match the regexp to each string in the hierarchy (using ‘replace_each_string`). If the regexp match, eliminate the matching string and substitute it with the pre_match, the result of the block, and the post_match

..., matched_string, ... -> ..., pre_match, block.call(match), post_match

the block might return arrays.



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/maruku/parse_span.rb', line 333

def map_match(regexp, &block)
	replace_each_string { |s| 
		processed = []
		while (match = regexp.match(s))
			# save the pre_match
			processed << match.pre_match if match.pre_match && match.pre_match.size>0
			# transform match
			result = block.call(match)
			# and append as processed
			[*result].each do |e| processed << e end
			# go on with the rest of the string
			s = match.post_match 
		end
		processed << s if s.size > 0
		processed
	}
end

#match_couple_of(open, close = nil, &block) ⇒ Object

Finds couple of delimiters in a hierarchy of Strings and MDElements

Open and close are two delimiters (like ‘[’ and ‘]’), or two Regexp.

If you don’t pass close, it defaults to open.

Each block is called with |contained children, match1, match2|



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/maruku/parse_span.rb', line 358

def match_couple_of(open, close=nil, &block)
	close = close || open
	 open_regexp =  open.kind_of?(Regexp) ?  open : Regexp.new(Regexp.escape(open))
	close_regexp = close.kind_of?(Regexp) ? close : Regexp.new(Regexp.escape(close))
	
	# Do the same to children first
	for c in @children; if c.kind_of? MDElement
		c.match_couple_of(open_regexp, close_regexp, &block)
	end end
	
	processed_children = []
	
	until @children.empty?
		c = @children.shift
		if c.kind_of? String
			match1 = open_regexp.match(c)
			if not match1
				processed_children << c
			else # we found opening, now search closing
#					puts "Found opening (#{marker}) in #{c.inspect}"
				# pre match is processed
				processed_children.push match1.pre_match if 
					match1.pre_match && match1.pre_match.size > 0
				# we will process again the post_match
				@children.unshift match1.post_match if 
					match1.post_match && match1.post_match.size>0
				
				contained = []; found_closing = false
				until @children.empty?  || found_closing
					c = @children.shift
					if c.kind_of? String
						match2 = close_regexp.match(c)
						if not match2 
							contained << c
						else
							# we found closing
							found_closing = true
							# pre match is contained
							contained.push match2.pre_match if 
								match2.pre_match && match2.pre_match.size>0
							# we will process again the post_match
							@children.unshift match2.post_match if 
								match2.post_match && match2.post_match.size>0

							# And now we call the block
							substitute = block.call(contained, match1, match2) 
							processed_children  << substitute
							
#								puts "Found closing (#{marker}) in #{c.inspect}"
#								puts "Children: #{contained.inspect}"
#								puts "Substitute: #{substitute.inspect}"
						end
					else
						contained << c
					end
				end
				
				if not found_closing
					# $stderr.puts "##### Could not find closing for #{open}, #{close} -- ignoring"
					processed_children << match1.to_s
					contained.reverse.each do |c|
						@children.unshift c
					end
				end
			end
		else
			processed_children << c
		end
	end
	
	raise "BugBug" unless @children.empty?
	
	rebuilt = []
	# rebuild strings
	processed_children.each do |c|
		if c.kind_of?(String) && rebuilt.last && rebuilt.last.kind_of?(String)
			rebuilt.last << c
		else
			rebuilt << c
		end
	end
	@children = rebuilt
end

#obfuscate(s) ⇒ Object

Email address



256
257
258
259
260
261
262
# File 'lib/maruku/to_html.rb', line 256

def obfuscate(s)
	res = ''
	s.each_byte do |char|
		res +=  "&#%03d;" % char
	end
	res
end

#replace_each_string(&block) ⇒ Object

Apply passed block to each String in the hierarchy.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/maruku/parse_span.rb', line 304

def replace_each_string(&block)
	for c in @children
		if c.kind_of? MDElement
			c.replace_each_string(&block)
		end
	end
	
	processed = []
	until @children.empty?
		c = @children.shift
		if c.kind_of? String
			result = block.call(c)
			[*result].each do |e| processed << e end
		else
			processed << c
		end
	end
	@children = processed
end

#source2html(source) ⇒ Object



159
160
161
162
163
# File 'lib/maruku/to_html.rb', line 159

def source2html(source)
	source = source.gsub(/&/,'&amp;')
	source = Text.normalize(source)
	Text.new(source, true, nil, false )
end

#to_html_abbreviationObject



314
315
316
317
318
319
# File 'lib/maruku/to_html.rb', line 314

def to_html_abbreviation
	abbr = Element.new 'abbr'
	abbr << Text.new(children[0])
	abbr.attributes['title'] = self.meta[:title] if self.meta[:title]
	abbr
end

#to_html_cellObject



386
# File 'lib/maruku/to_html.rb', line 386

def to_html_cell; wrap_as_element('td') end

#to_html_codeObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/maruku/to_html.rb', line 165

def to_html_code; 
	source = self.meta[:raw_code]	

	lang = self.meta[:lang] || @doc.meta[:code_lang] 

	lang = 'xml' if lang=='html'
	use_syntax = @doc.meta[:html_use_syntax] 
	
	element = 
	if use_syntax && lang
		convertor = Syntax::Convertors::HTML.for_syntax lang
		html = convertor.convert( source )
		
		show_spaces = get_setting(:code_show_spaces) 
		if show_spaces
			s.gsub!(/\t/,'&raquo;'+'&nbsp;'*3)
			s.gsub!(/ /,'&not;')
		end
		
#			puts "html: #{html}"
		pre = Document.new(html, {:respect_whitespace =>:all}).root
		pre.attributes['class'] = lang
#			puts "After: #{pre}"
		pre
	else
		pre = Element.new 'pre'
		s = source
		
		s  = s.gsub(/&/,'&amp;')
		s = Text.normalize(s)

		show_spaces = get_setting(:code_show_spaces) 
		if show_spaces
			s.gsub!(/\t/,'&raquo;'+'&nbsp;'*3)
			s.gsub!(/ /,'&not;')
		end

		text = Text.new(s, true, nil, false )
		
		pre << text
		pre
	end
	
	color = get_setting(:code_background_color,DEFAULT_CODE_COLOR)
	if color
		element.attributes['style'] = "background-color: #{color};"
	end
	element
end

#to_html_definitionObject



345
346
347
# File 'lib/maruku/to_html.rb', line 345

def to_html_definition		
	children_to_html
end

#to_html_definition_dataObject



349
# File 'lib/maruku/to_html.rb', line 349

def to_html_definition_data; wrap_as_element('dd') end

#to_html_definition_listObject

Definition lists ###



342
343
344
# File 'lib/maruku/to_html.rb', line 342

def to_html_definition_list
	wrap_as_element('dl')
end

#to_html_definition_termObject



348
# File 'lib/maruku/to_html.rb', line 348

def to_html_definition_term; wrap_as_element('dt') end

#to_html_email_addressObject



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/maruku/to_html.rb', line 264

def to_html_email_address
	email = @meta[:email]
	a = Element.new 'a'
		#a.attributes['href'] = Text.new("mailto:"+obfuscate(email),false,nil,true)
		#a.attributes.add Attribute.new('href',Text.new(
		#"mailto:"+obfuscate(email),false,nil,true))
		# Sorry, for the moment it doesn't work
		a.attributes['href'] = "mailto:#{email}"
		
		a << Text.new(obfuscate(email),false,nil,true)
	a
end

#to_html_emphasisObject



156
# File 'lib/maruku/to_html.rb', line 156

def to_html_emphasis;  wrap_as_element('em')               end

#to_html_footnote_referenceObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/maruku/to_html.rb', line 321

def to_html_footnote_reference
	id = @meta[:footnote_id]
	
	# save the order of used footnotes
	order = (@doc.meta[:footnotes_used] ||= [])
	
	# take next number
	order << id
	num = order.size; 
	
	sup = Element.new 'sup'
	sup.attributes['id'] = "fnref:#{num}"
		a = Element.new 'a'
		a << Text.new(num.to_s)
		a.attributes['href'] = "\#fn:#{num}"
		a.attributes['rel'] = 'footnote'
	sup << a
		
	sup
end

#to_html_head_cellObject



385
# File 'lib/maruku/to_html.rb', line 385

def to_html_head_cell; wrap_as_element('th') end

#to_html_headerObject



157
# File 'lib/maruku/to_html.rb', line 157

def to_html_header;    wrap_as_element "h#{@meta[:level]}" end

#to_html_hruleObject



129
# File 'lib/maruku/to_html.rb', line 129

def to_html_hrule; Element.new 'hr' end

#to_html_imageObject

Images



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/maruku/to_html.rb', line 279

def to_html_image
	a =  Element.new 'img'
	id = @meta[:ref_id]
	ref = @doc.refs[id]
	if not ref
		$stderr.puts "Could not find id = '#{id}'"
	else
		url = ref[:url]
		a.attributes['src'] = url
#			puts ref.inspect
		[:title, :class, :style].each do |s| 
			if ref[s] then
				a.attributes[s.to_s] = ref[s]
			end
		end
		
	end
	a
end


228
229
230
231
232
233
234
235
236
# File 'lib/maruku/to_html.rb', line 228

def to_html_immediate_link
	a = Element.new 'a'
	url = @meta[:url]
	text = url
	text = text.gsub(/^mailto:/,'') # don't show mailto
	a << Text.new(text)
	a.attributes['href'] = url
	a
end

#to_html_inline_codeObject



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/maruku/to_html.rb', line 215

def to_html_inline_code; 
	pre = Element.new 'tt'
		source = self.meta[:raw_code]
		pre << source2html(source) 
		
		color = get_setting(:code_background_color, DEFAULT_CODE_COLOR)
		if color
			pre.attributes['style'] = "background-color: #{color};"
		end
		
	pre
end

#to_html_liObject



152
# File 'lib/maruku/to_html.rb', line 152

def to_html_li;        wrap_as_element('li')               end

#to_html_li_spanObject



153
# File 'lib/maruku/to_html.rb', line 153

def to_html_li_span;   wrap_as_element('li')               end

#to_html_linebreakObject



130
# File 'lib/maruku/to_html.rb', line 130

def to_html_linebreak; Element.new 'br' end


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/maruku/to_html.rb', line 238

def to_html_link
	a =  wrap_as_element 'a'
	
	id = @meta[:ref_id]
	ref = @doc.refs[id]
	if not ref
		$stderr.puts "Could not find id = '#{id}'"
	else
		url = ref[:url]
		title = ref[:title]
		a.attributes['href'] = url
		a.attributes['title'] = title if title
	end
	a
end

#to_html_olObject



151
# File 'lib/maruku/to_html.rb', line 151

def to_html_ol;        wrap_as_element('ol')               end

#to_html_paragraphObject



149
# File 'lib/maruku/to_html.rb', line 149

def to_html_paragraph; wrap_as_element('p')                end

#to_html_quoteObject



154
# File 'lib/maruku/to_html.rb', line 154

def to_html_quote;     wrap_as_element('blockquote')       end

#to_html_raw_htmlObject



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/maruku/to_html.rb', line 299

def to_html_raw_html
	if @meta[:parsed_html]
		return @meta[:parsed_html].root
	else # invalid
		raw_html = @meta[:raw_html]
		# Creates red box with offending HTML
		$stderr.puts "Malformed HTML: #{raw_html}"
		div = Element.new('pre')
		div.attributes['style'] = 'border: solid 3px red; background-color: pink'
		div.attributes['class'] = 'markdown-html-error'
		div << Text.new("HTML parse error: \n#{raw_html}", true)
		return div
	end
end

#to_html_strongObject



155
# File 'lib/maruku/to_html.rb', line 155

def to_html_strong;    wrap_as_element('strong')           end

#to_html_tableObject

Table ###



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/maruku/to_html.rb', line 352

def to_html_table
	align = @meta[:align]
	num_columns = align.size

	head = @children.slice(0, num_columns)
	rows = []
	i = num_columns
	while i<@children.size
		rows << @children.slice(i, num_columns)
		i+=num_columns
	end
	
	table = create_html_element 'table'
		thead = Element.new 'thead'
		tr = Element.new 'tr'
			array_to_html(head).each do |x| tr<<x end
		thead << tr
		table << thead
		
		tbody = Element.new 'tbody'
		rows.each do |row|
			tr = Element.new 'tr'
				array_to_html(row).each_with_index do |x,i| 
					x.attributes['style'] ="text-align: #{align[i].to_s};" 
					tr<<x 
				end
					
			tbody << tr
		end
		table << tbody
	table
end

#to_html_ulObject



150
# File 'lib/maruku/to_html.rb', line 150

def to_html_ul;        wrap_as_element('ul')               end

#to_latex_abbreviationObject

def to_latex_definition_data; “” end



303
304
305
# File 'lib/maruku/to_latex.rb', line 303

def to_latex_abbreviation
	children_to_latex
end

#to_latex_cellObject



260
# File 'lib/maruku/to_latex.rb', line 260

def to_latex_cell; children_to_latex end

#to_latex_codeObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/maruku/to_latex.rb', line 99

def to_latex_code;
	raw_code = @meta[:raw_code] 
	
	if @doc.meta[:latex_use_listings]
#			puts @meta.inspect
		s = "\\lstset{columns=fixed,frame=shadowbox}"

		show_spaces = get_setting(:code_show_spaces) 
		if show_spaces
			s+= "\\lstset{showspaces=true,showtabs=true}\n"
		else
			s+= "\\lstset{showspaces=false,showtabs=false}\n"
		end
		
		color = latex_color get_setting(:code_background_color,DEFAULT_CODE_COLOR)
		
		s+= "\\lstset{backgroundcolor=#{color}}\n" 
		
		s+= "\\lstset{basicstyle=\\ttfamily\\footnotesize}\n" 
		
		
		lang = self.meta[:lang] || @doc.meta[:code_lang] || '{}'
		if lang
			s += "\\lstset{language=#{lang}}\n"
		end
		
		"#{s}\n\\begin{lstlisting}\n#{raw_code}\n\\end{lstlisting}"
	else
		"\\begin{verbatim}#{raw_code}\\end{verbatim}\n"
	end
end

#to_latex_definitionObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/maruku/to_latex.rb', line 285

def to_latex_definition		
	terms = @meta[:terms]
	definitions = @meta[:definitions]
	
	s = ""
	terms.each do |t|
		s +="\n\\item[#{t.children_to_latex}] "
	end

	definitions.each do |d|
		s += "#{d.children_to_latex} \n"
	end
	
	s
end

#to_latex_definition_listObject

Definition lists ###



278
279
280
281
282
283
# File 'lib/maruku/to_latex.rb', line 278

def to_latex_definition_list
	s = "\\begin{description}\n"
	s += children_to_latex
	s += "\\end{description}\n"
	s
end

#to_latex_email_addressObject



219
220
221
222
# File 'lib/maruku/to_latex.rb', line 219

def to_latex_email_address
	email = @meta[:email]
	"\\href{mailto:#{email}}{#{latex_escape(email)}}"
end

#to_latex_emphasisObject



165
# File 'lib/maruku/to_latex.rb', line 165

def to_latex_emphasis;  wrap_as_span('\em')               end

#to_latex_footnote_referenceObject



263
264
265
266
267
268
269
270
271
# File 'lib/maruku/to_latex.rb', line 263

def to_latex_footnote_reference
	id = @meta[:footnote_id]
	f = @doc.footnotes[id]
	if f
		"\\footnote{#{f.children_to_latex.strip}} "
	else
		$stderr.puts "Could not find footnote '#{fid}'"
	end
end

#to_latex_head_cellObject



259
# File 'lib/maruku/to_latex.rb', line 259

def to_latex_head_cell; children_to_latex end

#to_latex_headerObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/maruku/to_latex.rb', line 137

def to_latex_header
	h = TexHeaders[@meta[:level]] || 'paragraph'
	s = 
	# \hyperdef{category}{name}{text} 

	if @meta[:id]
		id = @meta[:id]
		if id[0,1] == '#' then id = [1,id.size] end
		%{\\hypertarget{%s}{}\\%s*{{%s}}\n\n} % [ id,h,  children_to_latex]
			
#			s + "\\hypertarget{user}{#{id}}{link}"
	else
		%{\\%s*{%s}\n\n} % [ h, children_to_latex]
	end
end

#to_latex_hruleObject



46
# File 'lib/maruku/to_latex.rb', line 46

def to_latex_hrule; "\n\\vspace{.5em} \\hrule \\vspace{.5em}\n" end

#to_latex_imageObject



307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/maruku/to_latex.rb', line 307

def to_latex_image
	id = @meta[:ref_id]
	ref = @doc.refs[id]
	if not ref
		$stderr.puts "Could not find id = '#{id}'"
		""
	else
		url = ref[:url]
		"{\\bf Images not supported yet (#{latex_escape(url)})}"
	end

end


195
196
197
198
# File 'lib/maruku/to_latex.rb', line 195

def to_latex_immediate_link
	url = @meta[:url]
	return "\\href{#{url}}{#{url.to_latex}}"
end

#to_latex_inline_codeObject



184
185
186
187
188
189
190
191
192
193
# File 'lib/maruku/to_latex.rb', line 184

def to_latex_inline_code; 
	source = self.meta[:raw_code]
	
	# Convert to printable latex chars (is much better than using \verb)
	s=latex_escape(source)
		
	color = latex_color(get_setting(:code_background_color,DEFAULT_CODE_COLOR),'colorbox')

	"#{color}{\\tt #{s}}"
end

#to_latex_liObject



157
158
159
# File 'lib/maruku/to_latex.rb', line 157

def to_latex_li;        
	"\\item #{children_to_latex}\n"  
end

#to_latex_li_spanObject



160
161
162
# File 'lib/maruku/to_latex.rb', line 160

def to_latex_li_span;
	"\\item #{children_to_latex}\n"
end

#to_latex_linebreakObject



47
# File 'lib/maruku/to_latex.rb', line 47

def to_latex_linebreak; "\\linebreak " end


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/maruku/to_latex.rb', line 200

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

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

#to_latex_olObject



156
# File 'lib/maruku/to_latex.rb', line 156

def to_latex_ol;        wrap_as_environment('enumerate')               end

#to_latex_paragraphObject



49
50
51
# File 'lib/maruku/to_latex.rb', line 49

def to_latex_paragraph; 
	children_to_latex+"\n\n"
end

#to_latex_quoteObject



153
# File 'lib/maruku/to_latex.rb', line 153

def to_latex_quote;        wrap_as_environment('quote')               end

#to_latex_raw_htmlObject



273
274
275
# File 'lib/maruku/to_latex.rb', line 273

def to_latex_raw_html
	'{\bf Raw HTML removed in latex version }'
end

#to_latex_strongObject



164
# File 'lib/maruku/to_latex.rb', line 164

def to_latex_strong;    wrap_as_span('\bf')           end

#to_latex_tableObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/maruku/to_latex.rb', line 225

def to_latex_table
	align = @meta[:align]
	num_columns = align.size

	head = @children.slice(0, num_columns)
	rows = []
	i = num_columns
	while i<@children.size
		rows << @children.slice(i, num_columns)
		i+=num_columns
	end
	
	h = {:center=>'c',:left=>'l',:right=>'r'}
	align_string = align.map{|a| h[a]}.join('|')
	
	s = "\\begin{tabular}{#{align_string}}\n"
		
		s += array_to_latex(head, '&') + "\\\\" +"\n"
		
		s += "\\hline \n"
		
		rows.each do |row|
			s += array_to_latex(row, '&') + "\\\\" +"\n"
		end
		
	s += "\\end{tabular}"
	
	# puts table in its own paragraph
	s += "\n\n"
	
	s
end

#to_latex_ulObject



155
# File 'lib/maruku/to_latex.rb', line 155

def to_latex_ul;        wrap_as_environment('itemize')               end

#wrap_as_element(name) ⇒ Object

renders children as html and wraps into an element of given name

Sets ‘id’ if meta is set



135
136
137
138
139
# File 'lib/maruku/to_html.rb', line 135

def wrap_as_element(name)
	m = create_html_element name
		children_to_html.each do |e| m << e; end
	m
end

#wrap_as_environment(name) ⇒ Object



171
172
173
174
175
# File 'lib/maruku/to_latex.rb', line 171

def wrap_as_environment(name)
"\\begin{#{name}}%
#{children_to_latex}
\\end{#{name}}\n"	
end

#wrap_as_span(c) ⇒ Object

def to_html_header; wrap_as_element “h#href=":level">meta” end



168
169
170
# File 'lib/maruku/to_latex.rb', line 168

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