Class: Jekyll::Prism

Inherits:
Liquid::Block
  • Object
show all
Includes:
Liquid::StandardFilters
Defined in:
lib/jekyll-prism.rb

Constant Summary collapse

LANGUAGE =
/^[a-z\-]+$/
OPTION_PARAMS =
/(file|highlight|numbering|language)(?:\s*=\s*("[a-zA-Z0-9.,\-\/\s]*"|\-?[0-9]+))?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Prism

Returns a new instance of Prism.



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
# File 'lib/jekyll-prism.rb', line 14

def initialize(tag_name, markup, tokens)
	super
	
	markup = markup.strip
	
	tmp = markup.split(/\s+/, 2)
	
	if tmp.count >= 1 then
		param = tmp[0].strip.downcase
		if LANGUAGE =~ param then
			@language = param
			if tmp.count == 2 then
				markup = tmp[1]
			else
				return
			end
		elsif not param.start_with? "file" then
			syntax_error
		end
	end
	
	
	params = markup.strip.scan(OPTION_PARAMS).to_a
	
	params.each { |x|
		if x.count == 1 || x[1] == nil then
			name = x[0]
			
			if name.eql? "numbering" then
				@numbering = true
			else
				syntax_error
			end
		elsif x.count == 2 then
			name = x[0]
			value = x[1]
			if name.eql? "file" then
				@file = value.tr('"', '')
			elsif name.eql? "highlight" then
				@highlight = value.tr('"', '')
			elsif name.eql? "numbering" then
				@numbering = value.to_i
			end
		else
			syntax_error
		end
	}
end

Instance Method Details

#render(context) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll-prism.rb', line 70

def render(context)
	code = super.to_s.strip
	dataline = ""
	
	linenumber = ""
	datastart = ""
	
	language = ""
	
	class_attr = ""
	
	unless @language.nil? then
		class_attr = class_attr + " language-" + @language + " "
	end
	
	if not @numbering.nil? then
		class_attr = class_attr + " line-numbers "
	end
	
	if @numbering.is_a? Numeric then
		datastart = "data-start='#{@numbering}'"
	end
	
	unless @highlight.nil? then
		dataline = "data-line='#{@highlight}'"
	end
	
	if @file.nil?
		"<pre class=\"#{class_attr}\" #{datastart} #{dataline}><code>#{code}</code></pre>"
	else
		"<pre class='#{class_attr}' #{datastart} #{dataline} data-src='#{@file}'></pre>"
	end
	
end

#syntax_errorObject

Raises:

  • (SyntaxError)


64
65
66
67
68
# File 'lib/jekyll-prism.rb', line 64

def syntax_error
	raise SyntaxError.new(
		"Syntax Error in 'prism' - Valid syntax: prism <lang> [<option>(=<value>)] or prism file=path/to/file [<option>(=<value>)]"
	)
end