Class: TDiary::Style::HTMLwithRouge

Inherits:
CommonMarker::HtmlRenderer
  • Object
show all
Defined in:
lib/tdiary/style/markdown.rb

Instance Method Summary collapse

Instance Method Details

#code_block(node) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/tdiary/style/markdown.rb', line 200

def code_block(node)
	language = if node.fence_info && !node.fence_info.empty?
					  node.fence_info.split(/\s+/)[0]
				  else
					  nil
				  end
	caption_part = ""
	language, caption = language.split(":", 2) if language
	if caption
		caption_part = "<span class=\"caption\">#{escape_html(caption)}</span>\n"
	end
	code = node.string_content
	lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText
	formatter = rouge_formatter(lexer)
	highlighted = formatter.format(lexer.lex(code))
	block do
		if option_enabled?(:GITHUB_PRE_LANG)
			out("<pre#{sourcepos(node)}")
			if language
				out(' lang="', language, '"')
			end
			out(">")
		else
			out("<pre#{sourcepos(node)}")
			if language
				out(' class="highlight ', language, '">')
			else
				out(' class="highlight plaintext">')
			end
		end
		out(caption_part)
		out('<code>')
		out(highlighted)
		out('</code></pre>')
	end
end

#image(node) ⇒ Object



237
238
239
240
241
242
243
244
245
246
# File 'lib/tdiary/style/markdown.rb', line 237

def image(node)
	out('<img src="', escape_href(node.url), '"')
	plain do
		out(' alt="', :children, '"')
	end
	if node.title && !node.title.empty?
		out(' title="', escape_html(node.title), '"')
	end
	out('>')
end

#rouge_formatter(lexer) ⇒ Object



248
249
250
# File 'lib/tdiary/style/markdown.rb', line 248

def rouge_formatter(lexer)
	::Rouge::Formatters::HTML.new(:css_class => "highlight #{lexer.tag}")
end