/
code_filter.rb
executable file
·41 lines (39 loc) · 1.18 KB
/
code_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'nanoc3'
class PreCodeFilter < Nanoc3::Filter
identifier :pre_code
type :text
def run(content, params={})
content.
gsub(/<pre><code>(.*?)(\n?)<\/code><\/pre>/m) do |match|
"<pre class=\"code\" title=\"triple click to select command\">#{$1.gsub(/\n/,"
")}</pre>"
end
end
end
class AutoIdsFilter < Nanoc3::Filter
identifier :auto_ids
type :text
def run(content, params={})
content.
gsub(/(<h[123]>)(.*?)(<\/h[123]>)/m) do |match|
__start = $1
__content = $2.strip
__end = $3
if
__content =~ /^(<a href=".*">)(.*<\/a>)$/m
then
"#{__start}#{$1}∞#{$2}#{__end}"
else
__id = create_id(__content)
"#{__start}<a class=\"anchor\" href=\"##{__id}\" name=\"#{__id}\">∞</a>#{__content}#{__end}"
end
end
end
def create_id(content)
content
.downcase # small leters
.gsub(/<.*?>/,'') # remove tags
.gsub(/[^[:alnum:] -]/,"") # only chars, numbers, spaces and dash
.gsub(/[ ]/, "-") # convert spaces to dashes
.gsub(/-+/, "-") # convert multiple dashes to single
end
end