forked from Polymer/old-docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude_external.rb
86 lines (80 loc) · 2.04 KB
/
include_external.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#
# Includes external code.
# Supports including named regions from a sample file.
#
# Usage:
#
# {% include_external filename %}
#
# or:
#
# {% include_external filename region-name %}
#
# Where regions are delimited by start and end tags like this:
#
# // [START region-name]
# // [END region-name]
#
# /* [START region-name] */
# /* [END region-name] */
#
# HTML-style comments are also supported:
#
# <!-- [START region-name] -->
# <!-- [END region-name] -->
#
# Regions may overlap. Region start and end tags are always omitted from the
# included content.
#
# When a region is selected, the region is de-indented as if the first
# non-whitespace character in the start tag is column 0.
#
#
module Jekyll
class IncludeExternal < Liquid::Tag
def initialize(tag_name, param_string, tokens)
params = param_string.split(" ").map(&:strip)
@filename = params[0]
@inregion = true
if (params.length > 1)
@region = params[1]
@inregion = false
end
@output = []
begin
startpattern = /^\s*(?:\/\/|\/\*|<!--)\s*\[START\s+(\S*)\s*\]/
endpattern = /^\s*(?:\/\/|\/\*|<!--)\s*\[END\s+(\S*)\s*\]/
f = File.new(@filename, "r")
text = f.read()
text.each_line do |line|
if m = startpattern.match(line)
if @region && @region == m[1]
@inregion = true
@base_indent = line[/^ */]
end
elsif m = endpattern.match(line)
if @region && @region == m[1]
@inregion = false
end
else
if @inregion
if @base_indent
line = line.sub(@base_indent,"")
end
@output << line
end
end
end
rescue => e
@output << "<div class=\"error\">IncludeExternal error: #{e}</div>"
ensure
f.close unless f.nil?
end
super
end
def render(context)
@output.join("")
end
end
end
Liquid::Template.register_tag('include_external', Jekyll::IncludeExternal)