Skip to content

Commit 1adeba9

Browse files
committed
Modify import task to add Jekyll highlight tags
1 parent b7cb00c commit 1adeba9

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

Rakefile

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ def url_to_path(url)
2727
return local_path
2828
end
2929

30+
def highlight(code, language_class)
31+
language = {
32+
'ruby-code' => 'ruby',
33+
'sh-code' => 'sh',
34+
'bash-code' => 'sh',
35+
'java-code' => 'java',
36+
'perl-code' => 'perl',
37+
'python-code' => 'python'
38+
}
39+
40+
lang = language[language_class]
41+
if lang
42+
"{% highlight #{lang} %}\n" <<
43+
code << "\n" <<
44+
"{% endhighlight %}"
45+
else
46+
code << "\n{: .code}"
47+
end
48+
end
49+
3050
def html_to_markdown(content_div)
3151
# remove all comments
3252
content_div.traverse do |node|
@@ -41,8 +61,25 @@ def html_to_markdown(content_div)
4161
span.replace(span.inner_text)
4262
end
4363

44-
# remove the 'class' attribute from all pre tags
45-
content_div.search('pre').remove_attr('class')
64+
# add Jekyll highlight tag to all pre elements
65+
# with class="code xy-code"
66+
content_div.search('pre.code').each do |pre|
67+
classes = pre['class'].split
68+
next unless classes.size == 2
69+
70+
lang = classes.reject {|e| e == 'code' }.first
71+
72+
# map all code elements to their inner_text
73+
pre.search('code').each do |code|
74+
code.replace(highlight(code.children.map { |node|
75+
if node.name == 'br'
76+
$/
77+
else
78+
node.inner_text
79+
end
80+
}.join, lang))
81+
end
82+
end
4683

4784
# map all code elements to their inner_text
4885
content_div.search('pre > code').each do |code|
@@ -55,6 +92,9 @@ def html_to_markdown(content_div)
5592
}.join)
5693
end
5794

95+
# remove the 'class' attribute from all pre tags
96+
content_div.search('pre').remove_attr('class')
97+
5898
# replace the #extended div with it's children
5999
if (extended_div = content_div.at('#extended'))
60100
extended_div.replace(extended_div.inner_html)

0 commit comments

Comments
 (0)