glyph 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORS.textile +1 -1
- data/CHANGELOG.textile +119 -222
- data/LICENSE.textile +1 -1
- data/README.textile +42 -23
- data/Rakefile +1 -3
- data/VERSION +1 -1
- data/benchmark.rb +72 -0
- data/book/config.yml +4 -4
- data/book/document.glyph +90 -57
- data/book/images/document_generation.png +0 -0
- data/book/lib/macros/reference.rb +75 -22
- data/book/output/html/glyph.html +3183 -2121
- data/book/output/html/images/document_generation.png +0 -0
- data/book/output/pdf/glyph.pdf +7370 -4913
- data/book/resources/document_generation.txt +34 -0
- data/book/snippets.yml +6 -0
- data/book/text/changelog.glyph +45 -34
- data/book/text/compiling/compiling.glyph +23 -0
- data/book/text/compiling/lite_mode.glyph +23 -0
- data/book/text/compiling/programmatic_usage.glyph +77 -0
- data/book/text/extending/bookmarks_headers.glyph +21 -0
- data/book/text/extending/further_reading.glyph +13 -0
- data/book/text/extending/internals.glyph +79 -0
- data/book/text/extending/interpreting.glyph +51 -0
- data/book/text/extending/macro_def.glyph +64 -0
- data/book/text/extending/params_attrs.glyph +70 -0
- data/book/text/extending/placeholders.glyph +34 -0
- data/book/text/extending/validators.glyph +16 -0
- data/book/text/getting_started/configuration.glyph +49 -0
- data/book/text/getting_started/create_project.glyph +41 -0
- data/book/text/getting_started/structure.glyph +55 -0
- data/book/text/introduction.glyph +49 -26
- data/book/text/license.glyph +1 -1
- data/book/text/macros/macros_block.glyph +99 -0
- data/book/text/macros/macros_core.glyph +208 -0
- data/book/text/macros/macros_filters.glyph +40 -0
- data/book/text/macros/macros_inline.glyph +50 -0
- data/book/text/macros/macros_structure.glyph +100 -0
- data/book/text/ref_commands.glyph +94 -73
- data/book/text/ref_config.glyph +34 -42
- data/book/text/ref_macros.glyph +1 -373
- data/book/text/text_editing/code.glyph +51 -0
- data/book/text/text_editing/conditionals.glyph +49 -0
- data/book/text/text_editing/evaluation.glyph +13 -0
- data/book/text/text_editing/glyph_files.glyph +7 -0
- data/book/text/text_editing/images.glyph +29 -0
- data/book/text/text_editing/inclusions.glyph +44 -0
- data/book/text/text_editing/links.glyph +53 -0
- data/book/text/text_editing/macro_intro.glyph +111 -0
- data/book/text/text_editing/raw_html.glyph +112 -0
- data/book/text/text_editing/sections.glyph +63 -0
- data/book/text/text_editing/stylesheets.glyph +36 -0
- data/book/text/troubleshooting/errors_command.glyph +39 -0
- data/book/text/troubleshooting/errors_generic.glyph +29 -0
- data/book/text/troubleshooting/errors_intro.glyph +3 -0
- data/book/text/troubleshooting/errors_macro.glyph +98 -0
- data/book/text/troubleshooting/errors_parser.glyph +29 -0
- data/config.yml +77 -58
- data/document.glyph +25 -25
- data/glyph.gemspec +57 -22
- data/lib/glyph.rb +54 -13
- data/lib/glyph/commands.rb +84 -17
- data/lib/glyph/config.rb +3 -3
- data/lib/glyph/document.rb +14 -8
- data/lib/glyph/interpreter.rb +18 -58
- data/lib/glyph/macro.rb +160 -55
- data/lib/glyph/macro_validators.rb +104 -12
- data/lib/glyph/node.rb +24 -0
- data/lib/glyph/parser.rb +278 -0
- data/lib/glyph/syntax_node.rb +225 -0
- data/macros/core.rb +212 -0
- data/macros/filters.rb +66 -15
- data/macros/html/block.rb +43 -105
- data/macros/html/inline.rb +11 -12
- data/macros/html/structure.rb +123 -58
- data/macros/xml.rb +33 -0
- data/spec/files/container.textile +2 -2
- data/spec/files/document.glyph +2 -2
- data/spec/files/document_with_toc.glyph +3 -3
- data/spec/files/included.textile +1 -1
- data/spec/files/ligature.jpg +0 -0
- data/spec/files/markdown.markdown +2 -1
- data/spec/lib/commands_spec.rb +46 -3
- data/spec/lib/document_spec.rb +4 -4
- data/spec/lib/glyph_spec.rb +17 -46
- data/spec/lib/interpreter_spec.rb +6 -25
- data/spec/lib/macro_spec.rb +141 -43
- data/spec/lib/macro_validators_spec.rb +27 -5
- data/spec/lib/node_spec.rb +26 -1
- data/spec/lib/parser_spec.rb +246 -0
- data/spec/lib/syntax_node_spec.rb +111 -0
- data/spec/macros/core_spec.rb +195 -0
- data/spec/macros/filters_spec.rb +38 -4
- data/spec/macros/macros_spec.rb +20 -176
- data/spec/macros/textile_spec.rb +13 -71
- data/spec/macros/xml_spec.rb +77 -0
- data/spec/spec_helper.rb +50 -10
- data/spec/tasks/load_spec.rb +13 -2
- data/styles/default.css +18 -6
- data/styles/pagination.css +1 -19
- data/tasks/generate.rake +2 -2
- data/tasks/load.rake +27 -17
- data/tasks/project.rake +1 -1
- metadata +75 -62
- data/book/script/compile.rb +0 -8
- data/book/script/prof +0 -1
- data/book/script/prof_results.htm +0 -21079
- data/book/text/authoring.glyph +0 -548
- data/book/text/extending.glyph +0 -224
- data/book/text/getting_started.glyph +0 -158
- data/book/text/troubleshooting.glyph +0 -179
- data/lib/glyph/glyph_language.rb +0 -538
- data/lib/glyph/glyph_language.treetop +0 -27
- data/macros/common.rb +0 -160
@@ -0,0 +1,225 @@
|
|
1
|
+
module Glyph
|
2
|
+
|
3
|
+
# A subclass of Glyph::SyntaxNode is instantiated by Glyph::Parser whenever a known
|
4
|
+
# text element is parsed.
|
5
|
+
# @since 0.3.0
|
6
|
+
class SyntaxNode < Node
|
7
|
+
|
8
|
+
# @return [String] an empty string
|
9
|
+
# @since 0.3.0
|
10
|
+
def to_s
|
11
|
+
""
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String] the value of the :value key
|
15
|
+
# @since 0.3.0
|
16
|
+
def evaluate(context, options={})
|
17
|
+
self[:value]
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Glyph::MacroNode] the first Glyph::MacroNode ancestor
|
21
|
+
# @since 0.3.0
|
22
|
+
def parent_macro
|
23
|
+
find_parent{|n| n.is_a?(MacroNode)}
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
# The root element of any Glyph Abstract Syntax Tree
|
29
|
+
# @since 0.3.0
|
30
|
+
class DocumentNode < SyntaxNode
|
31
|
+
|
32
|
+
# @return [String] the value of the children node
|
33
|
+
# @since 0.3.0
|
34
|
+
def evaluate(context)
|
35
|
+
self[:value] = ""
|
36
|
+
self.children.each {|c| self[:value] << c.evaluate(context) }
|
37
|
+
self[:value]
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
# A Glyph macro in Glyph Abstract Syntax Tree
|
43
|
+
# @since 0.3.0
|
44
|
+
class MacroNode < SyntaxNode
|
45
|
+
|
46
|
+
# @return [String] a textual representation of the macro
|
47
|
+
# @since 0.3.0
|
48
|
+
def to_s
|
49
|
+
e = self[:escape] ? "=" : ""
|
50
|
+
"#{self[:name]}["+e+attributes.join+parameters.join("|")+e+"]"
|
51
|
+
end
|
52
|
+
|
53
|
+
# Expands the macro
|
54
|
+
# @return [String] the value of the macro
|
55
|
+
# @since 0.3.0
|
56
|
+
def evaluate(context, options={})
|
57
|
+
self[:value] = expand(context)
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [Array<Glyph::ParameterNode>] an array of the child parameter nodes
|
61
|
+
# @since 0.3.0
|
62
|
+
def parameters
|
63
|
+
children.select{|n| n.is_a? ParameterNode }
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Returns the parameter syntax node at the specified index
|
68
|
+
# @param [Fixnum] n the index of the parameter
|
69
|
+
# @return [Glyph::ParameterNode, nil] a parameter node
|
70
|
+
# @since 0.3.0
|
71
|
+
def parameter(n)
|
72
|
+
parameters[n]
|
73
|
+
end
|
74
|
+
|
75
|
+
# Equivalent to Glyph::MacroNode#parameter(0).
|
76
|
+
# @since 0.3.0
|
77
|
+
def value
|
78
|
+
parameter(0)
|
79
|
+
end
|
80
|
+
|
81
|
+
# @return [Array<Glyph::AttributeNode>] an array of the child attribute nodes
|
82
|
+
# @since 0.3.0
|
83
|
+
def attributes
|
84
|
+
children.select{|n| n.is_a? AttributeNode }
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns the attribute syntax node with the specified name
|
88
|
+
# @param [Symbol] name the name of the attribute
|
89
|
+
# @return [Glyph::AttributeNode, nil] an attribute node
|
90
|
+
# @since 0.3.0
|
91
|
+
def attribute(name)
|
92
|
+
attributes.select{|n| n[:name] == name}[0]
|
93
|
+
end
|
94
|
+
|
95
|
+
alias attr attribute
|
96
|
+
alias param parameter
|
97
|
+
alias attrs attributes
|
98
|
+
alias params parameters
|
99
|
+
|
100
|
+
# Expands the macro corresponding to self[:name]
|
101
|
+
# @param [Glyph::MacroNode] context the context of the macro
|
102
|
+
# @return [String] the value of the macro
|
103
|
+
# @since 0.3.0
|
104
|
+
def expand(context)
|
105
|
+
xml_element(context)
|
106
|
+
self.merge!({
|
107
|
+
:source => context[:source],
|
108
|
+
:document => context[:document],
|
109
|
+
:info => context[:info],
|
110
|
+
:value => ""
|
111
|
+
})
|
112
|
+
Glyph::Macro.new(self).expand
|
113
|
+
end
|
114
|
+
|
115
|
+
protected
|
116
|
+
|
117
|
+
def xml_element(context)
|
118
|
+
known_macro = Glyph::MACROS.include? self[:name]
|
119
|
+
name = self[:name].to_s
|
120
|
+
if !known_macro && name.match(/^=(.+)/) then
|
121
|
+
# Force tag name override if macro starts with a '='
|
122
|
+
name.gsub! /^=(.+)/, '\1'
|
123
|
+
end
|
124
|
+
case
|
125
|
+
# Use XML syntax
|
126
|
+
when Glyph['language.set'] == 'xml' then
|
127
|
+
self[:element] = name
|
128
|
+
self[:name] = :"|xml|"
|
129
|
+
# Fallback to XML syntax
|
130
|
+
when Glyph['language.options.xml_fallback'] then
|
131
|
+
unless known_macro then
|
132
|
+
self[:element] = name
|
133
|
+
self[:fallback] = true
|
134
|
+
self[:name] = :"|xml|"
|
135
|
+
end
|
136
|
+
else
|
137
|
+
# Unknown macro
|
138
|
+
raise RuntimeError, "Undefined macro '#{name}'\n -> source: #{context[:source][:name]}" unless known_macro
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
# A piece of text in Glyph Abstract Syntax Tree
|
145
|
+
# @since 0.3.0
|
146
|
+
class TextNode < SyntaxNode
|
147
|
+
|
148
|
+
# @return [String] the text itself
|
149
|
+
def to_s
|
150
|
+
self[:value]
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
# A Glyph macro parameter in Glyph Abstract Syntax Tree
|
156
|
+
# @since 0.3.0
|
157
|
+
class ParameterNode < SyntaxNode
|
158
|
+
|
159
|
+
# @return [String] a textual representation of the parameter node
|
160
|
+
# @since 0.3.0
|
161
|
+
def to_s
|
162
|
+
children.join
|
163
|
+
end
|
164
|
+
|
165
|
+
# @return [String] a textual representation of the parameter contents
|
166
|
+
# @since 0.3.0
|
167
|
+
def contents
|
168
|
+
parent[:escape] ? ".[=#{children.join}=]" : children.join
|
169
|
+
end
|
170
|
+
|
171
|
+
# @param [Glyph::MacroNode] context the context of the macro
|
172
|
+
# @param [Hash] options a hash of options
|
173
|
+
# @option options [Boolean] :params whether to evaluate child nodes or not
|
174
|
+
# @return [String] the evaluated child nodes
|
175
|
+
# @since 0.3.0
|
176
|
+
def evaluate(context, options={:params => false})
|
177
|
+
self[:value] = ""
|
178
|
+
self.children.each {|c| self[:value] << c.evaluate(context) } if options[:params]
|
179
|
+
self[:value]
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
# A Glyph macro attribute in Glyph Abstract Syntax Tree
|
185
|
+
# @since 0.3.0
|
186
|
+
class AttributeNode < SyntaxNode
|
187
|
+
|
188
|
+
# @return [String] a textual representation of the attribute node
|
189
|
+
# @since 0.3.0
|
190
|
+
def to_s
|
191
|
+
e = self[:escape] ? "=" : ""
|
192
|
+
"@#{self[:name]}["+e+children.join+e+"]"
|
193
|
+
end
|
194
|
+
|
195
|
+
# @return [String] a textual representation of the attribute contents
|
196
|
+
# @since 0.3.0
|
197
|
+
def contents
|
198
|
+
self[:escape] ? ".[=#{children.join}=]" : children.join
|
199
|
+
end
|
200
|
+
|
201
|
+
# @param [Glyph::MacroNode] context the context of the macro
|
202
|
+
# @param [Hash] options a hash of options
|
203
|
+
# @option options [Boolean] :attrs whether to evaluate child nodes or not
|
204
|
+
# @return [String] the evaluated child nodes
|
205
|
+
# @since 0.3.0
|
206
|
+
def evaluate(context, options={:attrs => false})
|
207
|
+
self[:value] = ""
|
208
|
+
self.children.each {|c| self[:value] << c.evaluate(context) } if options[:attrs]
|
209
|
+
self[:value]
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
# Some escaped text within Glyph Abstract Syntax Tree
|
215
|
+
# @since 0.3.0
|
216
|
+
class EscapeNode < SyntaxNode
|
217
|
+
|
218
|
+
# Returns the escaped value
|
219
|
+
def to_s
|
220
|
+
self[:value]
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
data/macros/core.rb
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
macro :snippet do
|
4
|
+
no_mutual_inclusion_in 0
|
5
|
+
ident = value.to_sym
|
6
|
+
if Glyph::SNIPPETS.has_key? ident then
|
7
|
+
begin
|
8
|
+
update_source "snippet[#{ident}]"
|
9
|
+
interpret Glyph::SNIPPETS[ident]
|
10
|
+
rescue Exception => e
|
11
|
+
case
|
12
|
+
when e.is_a?(Glyph::MutualInclusionError) then
|
13
|
+
raise
|
14
|
+
when e.is_a?(Glyph::MacroError) then
|
15
|
+
macro_warning e.message, e
|
16
|
+
else
|
17
|
+
macro_warning e.message, e
|
18
|
+
end
|
19
|
+
macro_todo "Correct errors in snippet '#{ident}'"
|
20
|
+
end
|
21
|
+
else
|
22
|
+
macro_warning "Snippet '#{ident}' does not exist"
|
23
|
+
"[SNIPPET '#{ident}' NOT PROCESSED]"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
macro "snippet:" do
|
28
|
+
exact_parameters 2
|
29
|
+
ident = param(0)
|
30
|
+
text = param(1)
|
31
|
+
Glyph::SNIPPETS[ident.to_sym] = text
|
32
|
+
""
|
33
|
+
end
|
34
|
+
|
35
|
+
macro "macro:" do
|
36
|
+
safety_check
|
37
|
+
exact_parameters 2
|
38
|
+
ident = param(0)
|
39
|
+
code = param(1)
|
40
|
+
Glyph.macro(ident) do
|
41
|
+
instance_eval code
|
42
|
+
end
|
43
|
+
""
|
44
|
+
end
|
45
|
+
|
46
|
+
macro :include do
|
47
|
+
safety_check
|
48
|
+
exact_parameters 1
|
49
|
+
no_mutual_inclusion_in 0
|
50
|
+
v = value
|
51
|
+
v += ".glyph" unless v.match(/\..+$/)
|
52
|
+
ext = v.match(/\.(.*)$/)[1]
|
53
|
+
if Glyph.lite? then
|
54
|
+
file = Pathname.new(v)
|
55
|
+
else
|
56
|
+
if ext == 'rb' then
|
57
|
+
file = Glyph::PROJECT/"lib/#{v}"
|
58
|
+
else
|
59
|
+
file = Glyph::PROJECT/"text/#{v}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
if file.exist? then
|
63
|
+
contents = file_load file
|
64
|
+
if ext == "rb" then
|
65
|
+
begin
|
66
|
+
Glyph.instance_eval contents
|
67
|
+
""
|
68
|
+
rescue Exception => e
|
69
|
+
macro_warning e.message, e
|
70
|
+
end
|
71
|
+
else
|
72
|
+
if Glyph["filters.by_file_extension"] && !ext.in?(['rb','glyph']) then
|
73
|
+
if Glyph::MACROS.include?(ext.to_sym) then
|
74
|
+
contents = "#{ext}[#{contents}]"
|
75
|
+
else
|
76
|
+
macro_warning "Filter macro '#{ext}' not available"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
begin
|
80
|
+
update_source v
|
81
|
+
interpret contents
|
82
|
+
rescue Glyph::MutualInclusionError => e
|
83
|
+
raise
|
84
|
+
rescue Glyph::MacroError => e
|
85
|
+
macro_warning e.message, e
|
86
|
+
macro_todo "Correct errors in file '#{value}'"
|
87
|
+
rescue Exception => e
|
88
|
+
macro_warning e.message, e
|
89
|
+
macro_todo "Correct errors in file '#{value}'"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
else
|
93
|
+
macro_warning "File '#{value}' no found."
|
94
|
+
"[FILE '#{value}' NOT FOUND]"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
macro :ruby do
|
99
|
+
safety_check
|
100
|
+
max_parameters 1
|
101
|
+
res = Glyph.instance_eval(value.gsub(/\\*([\[\]\|])/){$1})
|
102
|
+
res.is_a?(Proc) ? "" : res
|
103
|
+
end
|
104
|
+
|
105
|
+
macro :config do
|
106
|
+
Glyph[value]
|
107
|
+
end
|
108
|
+
|
109
|
+
macro "config:" do
|
110
|
+
safety_check
|
111
|
+
max_parameters 2
|
112
|
+
setting = param(0)
|
113
|
+
v = param(1) rescue nil
|
114
|
+
if setting.match /^system\..+/ then
|
115
|
+
macro_warning "Cannot reset '#{setting}' setting (system use only)."
|
116
|
+
else
|
117
|
+
Glyph[setting] = v
|
118
|
+
end
|
119
|
+
nil
|
120
|
+
end
|
121
|
+
|
122
|
+
macro :comment do
|
123
|
+
""
|
124
|
+
end
|
125
|
+
|
126
|
+
macro :escape do
|
127
|
+
value
|
128
|
+
end
|
129
|
+
|
130
|
+
macro :condition do
|
131
|
+
min_parameters 1
|
132
|
+
max_parameters 2
|
133
|
+
res = param(0)
|
134
|
+
(res.blank? || res == "false") ? "" : param(1).to_s
|
135
|
+
end
|
136
|
+
|
137
|
+
macro :eq do
|
138
|
+
min_parameters 1
|
139
|
+
max_parameters 2
|
140
|
+
(param(0).to_s == param(1).to_s) ? true : nil
|
141
|
+
end
|
142
|
+
|
143
|
+
macro :not do
|
144
|
+
max_parameters 1
|
145
|
+
v = param(0).to_s
|
146
|
+
(v.blank? || v == "false") ? true : nil
|
147
|
+
end
|
148
|
+
|
149
|
+
macro :and do
|
150
|
+
min_parameters 1
|
151
|
+
max_parameters 2
|
152
|
+
res_a = !param(0).blank?
|
153
|
+
res_b = !param(1).blank?
|
154
|
+
(res_a && res_b) ? true : nil
|
155
|
+
end
|
156
|
+
|
157
|
+
macro :or do
|
158
|
+
min_parameters 1
|
159
|
+
max_parameters 2
|
160
|
+
res_a = !param(0).blank?
|
161
|
+
res_b = !param(1).blank?
|
162
|
+
(res_a || res_b) ? true : nil
|
163
|
+
end
|
164
|
+
|
165
|
+
macro :match do
|
166
|
+
exact_parameters 2
|
167
|
+
val = param(0).to_s
|
168
|
+
regexp = param(1).to_s
|
169
|
+
macro_error "Invalid regular expression: #{regexp}" unless regexp.match /^\/.*\/[a-z]?$/
|
170
|
+
val.match(instance_eval(regexp)) ? true : nil
|
171
|
+
end
|
172
|
+
|
173
|
+
macro "alias:" do
|
174
|
+
exact_parameters 2
|
175
|
+
Glyph.macro_alias param(0) => param(1)
|
176
|
+
end
|
177
|
+
|
178
|
+
macro "rewrite:" do
|
179
|
+
safety_check
|
180
|
+
exact_parameters 2
|
181
|
+
macro_name = param(0).to_sym
|
182
|
+
@node.param(1).descend do |n, level|
|
183
|
+
if n[:name] == macro_name then
|
184
|
+
macro_error "Macro '#{macro_name}' cannot be defined by itself"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
string = raw_param(1)
|
188
|
+
Glyph.macro macro_name do
|
189
|
+
s = string.dup
|
190
|
+
# Parameters
|
191
|
+
s.gsub!(/\{\{(\d+)\}\}/) do
|
192
|
+
raw_param($1.to_i).strip
|
193
|
+
end
|
194
|
+
# Attributes
|
195
|
+
s.gsub!(/\{\{([^\[\]\|\\\s]+)\}\}/) do
|
196
|
+
raw_attr($1.to_sym).strip
|
197
|
+
end
|
198
|
+
interpret s
|
199
|
+
end
|
200
|
+
nil
|
201
|
+
end
|
202
|
+
|
203
|
+
macro_alias '--' => :comment
|
204
|
+
macro_alias '&' => :snippet
|
205
|
+
macro_alias '&:' => 'snippet:'
|
206
|
+
macro_alias '%:' => 'macro:'
|
207
|
+
macro_alias '%' => :ruby
|
208
|
+
macro_alias '$' => :config
|
209
|
+
macro_alias '$:' => 'config:'
|
210
|
+
macro_alias '.' => :escape
|
211
|
+
macro_alias '?' => :condition
|
212
|
+
macro_alias 'rw:' => 'rewrite:'
|
data/macros/filters.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
macro :textile do
|
4
|
-
exact_parameters 1
|
4
|
+
exact_parameters 1
|
5
5
|
rc = nil
|
6
6
|
begin
|
7
7
|
require 'RedCloth'
|
8
|
-
rc = RedCloth.new
|
8
|
+
rc = RedCloth.new value, Glyph::CONFIG.get("filters.redcloth.restrictions")
|
9
9
|
rescue Exception
|
10
|
-
macro_error "RedCloth gem not installed. Please run: gem
|
10
|
+
macro_error "RedCloth gem not installed. Please run: gem install RedCloth"
|
11
11
|
end
|
12
12
|
target = Glyph["filters.target"]
|
13
13
|
case target.to_sym
|
@@ -24,7 +24,10 @@ macro :markdown do
|
|
24
24
|
exact_parameters 1, :level => :warning
|
25
25
|
md = nil
|
26
26
|
markdown_converter = Glyph["filters.markdown_converter"].to_sym rescue nil
|
27
|
-
|
27
|
+
begin
|
28
|
+
raise LoadError unless markdown_converter
|
29
|
+
require markdown_converter.to_s
|
30
|
+
rescue LoadError
|
28
31
|
begin
|
29
32
|
require 'bluecloth'
|
30
33
|
markdown_converter = :bluecloth
|
@@ -41,28 +44,24 @@ macro :markdown do
|
|
41
44
|
require 'kramdown'
|
42
45
|
markdown_converter = :kramdown
|
43
46
|
rescue LoadError
|
44
|
-
macro_error "No MarkDown converter installed. Please run: gem install bluecloth"
|
47
|
+
macro_error "No supported MarkDown converter installed. Please run: gem install bluecloth"
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
49
|
-
Glyph["filters.markdown_converter"] = markdown_converter
|
50
52
|
end
|
53
|
+
Glyph["filters.markdown_converter"] = markdown_converter
|
51
54
|
case markdown_converter
|
52
55
|
when :bluecloth
|
53
|
-
|
54
|
-
md = BlueCloth.new @value
|
56
|
+
md = BlueCloth.new value
|
55
57
|
when :rdiscount
|
56
|
-
|
57
|
-
md = RDiscount.new @value
|
58
|
+
md = RDiscount.new value
|
58
59
|
when :maruku
|
59
|
-
|
60
|
-
md = Maruku.new @value
|
60
|
+
md = Maruku.new value
|
61
61
|
when :kramdown
|
62
|
-
|
63
|
-
md = Kramdown::Document.new @value
|
62
|
+
md = Kramdown::Document.new value
|
64
63
|
else
|
65
|
-
macro_error "No MarkDown converter installed. Please run: gem
|
64
|
+
macro_error "No supported MarkDown converter installed. Please run: gem install bluecloth"
|
66
65
|
end
|
67
66
|
target = Glyph["filters.target"]
|
68
67
|
if target.to_sym == :html then
|
@@ -72,4 +71,56 @@ macro :markdown do
|
|
72
71
|
end
|
73
72
|
end
|
74
73
|
|
74
|
+
macro :highlight do
|
75
|
+
exact_parameters 2
|
76
|
+
lang = param(0)
|
77
|
+
text = param(1)
|
78
|
+
text.gsub!(/\\(.)/){$1}
|
79
|
+
highlighter = Glyph["filters.highlighter"].to_sym rescue nil
|
80
|
+
begin
|
81
|
+
raise LoadError unless highlighter
|
82
|
+
if highlighter.to_s.match(/^(uv|ultraviolet)$/) then
|
83
|
+
require 'uv'
|
84
|
+
else
|
85
|
+
require highlighter.to_s
|
86
|
+
end
|
87
|
+
rescue LoadError
|
88
|
+
begin
|
89
|
+
require 'coderay'
|
90
|
+
highlighter = :coderay
|
91
|
+
rescue LoadError
|
92
|
+
begin
|
93
|
+
require 'uv'
|
94
|
+
highlighter = :ultraviolet
|
95
|
+
rescue LoadError
|
96
|
+
macro_error "No supported highlighter installed. Please run: gem install coderay"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
Glyph["highlighter.current"] = highlighter
|
101
|
+
target = Glyph["filters.target"]
|
102
|
+
result = ""
|
103
|
+
case highlighter.to_sym
|
104
|
+
when :coderay
|
105
|
+
begin
|
106
|
+
result = CodeRay.scan(text, lang).div(Glyph["filters.coderay"])
|
107
|
+
rescue Exception => e
|
108
|
+
macro_error e.message
|
109
|
+
end
|
110
|
+
when :ultraviolet
|
111
|
+
begin
|
112
|
+
target = 'xhtml' if target.to_s == 'html'
|
113
|
+
result = Uv.parse(text.to_s, target.to_s, lang.to_s,
|
114
|
+
Glyph["filters.ultraviolet.line_numbers"],
|
115
|
+
Glyph["filters.ultraviolet.theme"].to_s)
|
116
|
+
rescue Exception => e
|
117
|
+
macro_error e.message
|
118
|
+
end
|
119
|
+
else
|
120
|
+
macro_error "No supported highlighter installed. Please run: gem install coderay"
|
121
|
+
end
|
122
|
+
result
|
123
|
+
end
|
124
|
+
|
75
125
|
macro_alias :md => :markdown
|
126
|
+
macro_alias :txt => :textile
|