kron4eg-wikicloth 0.1.3
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/MIT-LICENSE +20 -0
- data/README +0 -0
- data/Rakefile +23 -0
- data/init.rb +1 -0
- data/lib/core_ext.rb +43 -0
- data/lib/wiki_buffer.rb +279 -0
- data/lib/wiki_buffer/html_element.rb +237 -0
- data/lib/wiki_buffer/link.rb +70 -0
- data/lib/wiki_buffer/table.rb +159 -0
- data/lib/wiki_buffer/var.rb +77 -0
- data/lib/wiki_cloth.rb +61 -0
- data/lib/wiki_link_handler.rb +138 -0
- data/lib/wikicloth.rb +5 -0
- data/rails/init.rb +1 -0
- data/run_tests.rb +48 -0
- data/sample_documents/air_force_one.wiki +170 -0
- data/sample_documents/cheatsheet.wiki +205 -0
- data/sample_documents/default.css +34 -0
- data/sample_documents/elements.wiki +7 -0
- data/sample_documents/george_washington.wiki +526 -0
- data/sample_documents/images.wiki +15 -0
- data/sample_documents/lists.wiki +421 -0
- data/sample_documents/pipe_trick.wiki +68 -0
- data/sample_documents/random.wiki +55 -0
- data/sample_documents/tv.wiki +312 -0
- data/sample_documents/wiki.png +0 -0
- data/sample_documents/wiki_tables.wiki +410 -0
- data/tasks/wikicloth_tasks.rake +0 -0
- data/test/test_helper.rb +3 -0
- data/test/wiki_cloth_test.rb +8 -0
- data/wikicloth.gemspec +77 -0
- metadata +100 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
module WikiCloth
|
2
|
+
|
3
|
+
class WikiBuffer::Link < WikiBuffer
|
4
|
+
|
5
|
+
def initialize(data="",options={})
|
6
|
+
super(data,options)
|
7
|
+
@in_quotes = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def internal_link
|
11
|
+
@internal_link ||= false
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
link_handler = @options[:link_handler]
|
16
|
+
unless self.internal_link
|
17
|
+
return link_handler.external_link("#{params[0]}".strip, "#{params[1]}".strip)
|
18
|
+
else
|
19
|
+
case
|
20
|
+
when params[0] =~ /^:(.*)/
|
21
|
+
return link_handler.link_for(params[0],params[1])
|
22
|
+
when params[0] =~ /^\s*([a-zA-Z0-9-]+)\s*:(.*)$/
|
23
|
+
return link_handler.link_for_resource($1,$2,params[1..-1])
|
24
|
+
else
|
25
|
+
return link_handler.link_for(params[0],params[1])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
def internal_link=(val)
|
32
|
+
@internal_link = (val == true ? true : false)
|
33
|
+
end
|
34
|
+
|
35
|
+
def new_char()
|
36
|
+
case
|
37
|
+
# check if this link is internal or external
|
38
|
+
when previous_char.blank? && current_char == '['
|
39
|
+
self.internal_link = true
|
40
|
+
|
41
|
+
# Marks the beginning of another paramater for
|
42
|
+
# the current object
|
43
|
+
when current_char == '|' && self.internal_link == true && @in_quotes == false
|
44
|
+
self.current_param = self.data
|
45
|
+
self.data = ""
|
46
|
+
self.params << ""
|
47
|
+
|
48
|
+
# URL label
|
49
|
+
when current_char == ' ' && self.internal_link == false && params[1].nil? && !self.data.blank?
|
50
|
+
self.current_param = self.data
|
51
|
+
self.data = ""
|
52
|
+
self.params << ""
|
53
|
+
|
54
|
+
# end of link
|
55
|
+
when current_char == ']' && ((previous_char == ']' && self.internal_link == true) || self.internal_link == false) && @in_quotes == false
|
56
|
+
self.data.chop! if self.internal_link == true
|
57
|
+
self.current_param = self.data
|
58
|
+
self.data = ""
|
59
|
+
return false
|
60
|
+
|
61
|
+
else
|
62
|
+
self.data += current_char unless current_char == ' ' && self.data.blank?
|
63
|
+
end
|
64
|
+
|
65
|
+
return true
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
module WikiCloth
|
2
|
+
|
3
|
+
class WikiBuffer::Table < WikiBuffer
|
4
|
+
|
5
|
+
def initialize(data="",options={})
|
6
|
+
super(data,options)
|
7
|
+
self.buffer_type = "table"
|
8
|
+
@start_table = true
|
9
|
+
@start_row = false
|
10
|
+
@start_caption = false
|
11
|
+
@in_quotes = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def table_caption
|
15
|
+
@caption ||= ""
|
16
|
+
return @caption.kind_of?(Hash) ? @caption[:value] : @caption
|
17
|
+
end
|
18
|
+
|
19
|
+
def table_caption_attributes
|
20
|
+
@caption.kind_of?(Hash) ? @caption[:style] : ""
|
21
|
+
end
|
22
|
+
|
23
|
+
def rows
|
24
|
+
@rows ||= [ [] ]
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
row_count = 0
|
29
|
+
ret = "<table" + (params[0].blank? ? "" : " #{params[0].strip}") + ">"
|
30
|
+
ret += "<caption" + (self.table_caption_attributes.blank? ? "" : " #{table_caption_attributes.strip}") +
|
31
|
+
">#{table_caption.strip}</caption>" unless self.table_caption.blank?
|
32
|
+
for row in rows
|
33
|
+
row_count += 1
|
34
|
+
ret += "<tr" + (params[row_count].nil? || params[row_count].blank? ? "" : " #{params[row_count].strip}") + ">"
|
35
|
+
for cell in row
|
36
|
+
cell_attributes = cell[:style].blank? ? "" : " #{cell[:style].strip}"
|
37
|
+
ret += "<#{cell[:type]}#{cell_attributes}>\n#{cell[:value].strip}\n</#{cell[:type]}>"
|
38
|
+
end
|
39
|
+
ret += "</tr>"
|
40
|
+
end
|
41
|
+
ret += "</table>"
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
def rows=(val)
|
46
|
+
@rows = val
|
47
|
+
end
|
48
|
+
|
49
|
+
def table_caption_attributes=(val)
|
50
|
+
@caption = { :style => val, :value => self.table_caption } unless @caption.kind_of?(Hash)
|
51
|
+
@caption[:style] = val if @caption.kind_of?(Hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
def table_caption=(val)
|
55
|
+
@caption = val unless @caption.kind_of?(Hash)
|
56
|
+
@caption[:value] = val if @caption.kind_of?(Hash)
|
57
|
+
end
|
58
|
+
|
59
|
+
def next_row()
|
60
|
+
self.params << ""
|
61
|
+
self.rows << []
|
62
|
+
end
|
63
|
+
|
64
|
+
def next_cell(type="td")
|
65
|
+
if self.rows[-1].size == 0
|
66
|
+
self.rows[-1] = [ { :type => type, :value => "", :style => "" } ]
|
67
|
+
else
|
68
|
+
self.rows[-1][-1][:value] = self.data
|
69
|
+
self.rows[-1] << { :type => type, :value => "", :style => "" }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def new_char()
|
74
|
+
if @check_cell_data == 1
|
75
|
+
case
|
76
|
+
when current_char != '|' && @start_caption == false && self.rows[-1][-1][:style].blank?
|
77
|
+
self.rows[-1][-1][:style] = self.data
|
78
|
+
self.data = ""
|
79
|
+
when current_char != '|' && @start_caption == true && self.table_caption_attributes.blank?
|
80
|
+
self.table_caption_attributes = self.data
|
81
|
+
self.data = ""
|
82
|
+
end
|
83
|
+
@check_cell_data = 0
|
84
|
+
end
|
85
|
+
|
86
|
+
case
|
87
|
+
# Next table cell in row (TD)
|
88
|
+
when current_char == "|" && (previous_char == "\n" || previous_char == "|") && @in_quotes == false
|
89
|
+
self.data.chop!
|
90
|
+
self.next_cell() unless self.data.blank? && previous_char == "|"
|
91
|
+
self.data = ""
|
92
|
+
|
93
|
+
# Next table cell in row (TH)
|
94
|
+
when current_char == "!" && (previous_char == "\n" || previous_char == "!") && @in_quotes == false
|
95
|
+
self.data.chop!
|
96
|
+
self.next_cell('th')
|
97
|
+
self.data = ""
|
98
|
+
|
99
|
+
# End of a table
|
100
|
+
when current_char == '}' && previous_char == '|'
|
101
|
+
self.data = ""
|
102
|
+
self.rows[-1].pop
|
103
|
+
return false
|
104
|
+
|
105
|
+
# Start table caption
|
106
|
+
when current_char == '+' && previous_char == '|' && @in_quotes == false
|
107
|
+
self.data = ""
|
108
|
+
self.rows[-1].pop
|
109
|
+
@start_caption = true
|
110
|
+
|
111
|
+
# Table cell might have attributes
|
112
|
+
when current_char == '|' && previous_char != "\n" && @in_quotes == false
|
113
|
+
@check_cell_data = 1
|
114
|
+
|
115
|
+
# End table caption
|
116
|
+
when current_char == "\n" && @start_caption == true && @in_quotes == false
|
117
|
+
@start_caption = false
|
118
|
+
self.table_caption = self.data
|
119
|
+
self.data = ""
|
120
|
+
|
121
|
+
# in quotes
|
122
|
+
when current_char == '"' && previous_char != '\\'
|
123
|
+
@in_quotes = !@in_quotes
|
124
|
+
self.data += '"'
|
125
|
+
|
126
|
+
# Table params
|
127
|
+
when current_char == "\n" && @start_table == true && @in_quotes == false
|
128
|
+
@start_table = false
|
129
|
+
unless self.data.blank?
|
130
|
+
self.current_param = self.data
|
131
|
+
self.params << ""
|
132
|
+
end
|
133
|
+
self.data = ""
|
134
|
+
|
135
|
+
# Table row params
|
136
|
+
when current_char == "\n" && @start_row == true && @in_quotes == false
|
137
|
+
@start_row = false
|
138
|
+
unless self.data.blank?
|
139
|
+
self.current_param = self.data
|
140
|
+
end
|
141
|
+
self.data = ""
|
142
|
+
|
143
|
+
# Start new table row
|
144
|
+
when current_char == '-' && previous_char == '|' && @in_quotes == false
|
145
|
+
self.data.chop!
|
146
|
+
self.rows[-1].pop
|
147
|
+
self.next_row()
|
148
|
+
@start_row = true
|
149
|
+
|
150
|
+
else
|
151
|
+
self.data += current_char
|
152
|
+
end
|
153
|
+
|
154
|
+
return true
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module WikiCloth
|
2
|
+
|
3
|
+
class WikiBuffer::Var < WikiBuffer
|
4
|
+
|
5
|
+
def initialize(data="",options={})
|
6
|
+
super(data,options)
|
7
|
+
self.buffer_type = "var"
|
8
|
+
@in_quotes = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def skip_html?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def function_name
|
16
|
+
@fname
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
if self.is_function?
|
21
|
+
ret = "#{buffer_type}"
|
22
|
+
ret += " function #{function_name}"
|
23
|
+
ret += "(#{params.inspect})"
|
24
|
+
ret += " [#{data}]"
|
25
|
+
else
|
26
|
+
ret = @options[:link_handler].include_resource("#{params[0]}".strip,params[1..-1])
|
27
|
+
end
|
28
|
+
ret ||= "<!-- TEMPLATE[#{params[0]}] NOT FOUND -->"
|
29
|
+
ret
|
30
|
+
end
|
31
|
+
|
32
|
+
def is_function?
|
33
|
+
self.function_name.nil? || self.function_name.blank? ? false : true
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
def function_name=(val)
|
38
|
+
@fname = val
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_char()
|
42
|
+
case
|
43
|
+
when current_char == '|' && @in_quotes == false
|
44
|
+
self.current_param = self.data
|
45
|
+
self.data = ""
|
46
|
+
self.params << ""
|
47
|
+
|
48
|
+
# Start of either a function or a namespace change
|
49
|
+
when current_char == ':' && @in_quotes == false && self.params.size <= 1
|
50
|
+
self.function_name = self.data
|
51
|
+
self.data = ""
|
52
|
+
puts "[found var function (#{function_name})"
|
53
|
+
|
54
|
+
# Dealing with variable names within functions
|
55
|
+
# and variables
|
56
|
+
when current_char == '=' && @in_quotes == false
|
57
|
+
self.current_param = self.data
|
58
|
+
self.data = ""
|
59
|
+
self.name_current_param()
|
60
|
+
|
61
|
+
# End of a template, variable, or function
|
62
|
+
when current_char == '}' && previous_char == '}'
|
63
|
+
self.data.chop!
|
64
|
+
self.current_param = self.data
|
65
|
+
self.data = ""
|
66
|
+
return false
|
67
|
+
|
68
|
+
else
|
69
|
+
self.data += current_char
|
70
|
+
end
|
71
|
+
|
72
|
+
return true
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/lib/wiki_cloth.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'jcode'
|
2
|
+
|
3
|
+
module WikiCloth
|
4
|
+
|
5
|
+
class WikiCloth
|
6
|
+
|
7
|
+
def initialize(opt={})
|
8
|
+
self.load(opt[:data],opt[:params]) unless opt[:data].nil? || opt[:data].blank?
|
9
|
+
self.options[:link_handler] = opt[:link_handler] unless opt[:link_handler].nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
def load(data,p={})
|
13
|
+
data.gsub!(/<!--(.|\s)*?-->/,"")
|
14
|
+
self.params = p
|
15
|
+
self.html = data
|
16
|
+
end
|
17
|
+
|
18
|
+
def render(opt={})
|
19
|
+
self.options = { :output => :html, :link_handler => self.link_handler, :params => self.params }.merge(opt)
|
20
|
+
self.options[:link_handler].params = options[:params]
|
21
|
+
buffer = WikiBuffer.new("",options)
|
22
|
+
self.html.each_char { |c| buffer.add_char(c) }
|
23
|
+
buffer.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_html(opt={})
|
27
|
+
self.render(opt)
|
28
|
+
end
|
29
|
+
|
30
|
+
def link_handler
|
31
|
+
self.options[:link_handler] ||= WikiLinkHandler.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def html
|
35
|
+
@page_data
|
36
|
+
end
|
37
|
+
|
38
|
+
def params
|
39
|
+
@page_params ||= {}
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
def options=(val)
|
44
|
+
@options = val
|
45
|
+
end
|
46
|
+
|
47
|
+
def options
|
48
|
+
@options ||= {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def html=(val)
|
52
|
+
@page_data = val
|
53
|
+
end
|
54
|
+
|
55
|
+
def params=(val)
|
56
|
+
@page_params = val
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'builder'
|
3
|
+
|
4
|
+
module WikiCloth
|
5
|
+
|
6
|
+
class WikiLinkHandler
|
7
|
+
|
8
|
+
def references
|
9
|
+
@references ||= []
|
10
|
+
end
|
11
|
+
|
12
|
+
def section_link(section)
|
13
|
+
""
|
14
|
+
end
|
15
|
+
|
16
|
+
def params
|
17
|
+
@params ||= {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def external_links
|
21
|
+
@external_links ||= []
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_reference_by_name(n)
|
25
|
+
references.each { |r| return r if !r[:name].nil? && r[:name].strip == n }
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def reference_index(h)
|
30
|
+
references.each_index { |r| return r+1 if references[r] == h }
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def references=(val)
|
35
|
+
@references = val
|
36
|
+
end
|
37
|
+
|
38
|
+
def params=(val)
|
39
|
+
@params = val
|
40
|
+
end
|
41
|
+
|
42
|
+
def external_link(url,text)
|
43
|
+
self.external_links << url
|
44
|
+
elem.a({ :href => url }) { |x| x << (text.blank? ? url : text) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def external_links=(val)
|
48
|
+
@external_links = val
|
49
|
+
end
|
50
|
+
|
51
|
+
def url_for(page)
|
52
|
+
"javascript:void(0)"
|
53
|
+
end
|
54
|
+
|
55
|
+
def link_attributes_for(page)
|
56
|
+
{ :href => url_for(page) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def link_for(page, text)
|
60
|
+
ltitle = !text.nil? && text.blank? ? self.pipe_trick(page) : text
|
61
|
+
ltitle = page if text.nil?
|
62
|
+
elem.a(link_attributes_for(page)) { |x| x << ltitle.strip }
|
63
|
+
end
|
64
|
+
|
65
|
+
def include_resource(resource, options=[])
|
66
|
+
return self.params[resource] unless self.params[resource].nil?
|
67
|
+
end
|
68
|
+
|
69
|
+
def link_for_resource(prefix, resource, options=[])
|
70
|
+
ret = ""
|
71
|
+
prefix.downcase!
|
72
|
+
case
|
73
|
+
when ["image","file","media"].include?(prefix)
|
74
|
+
ret += wiki_image(resource,options)
|
75
|
+
else
|
76
|
+
title = options[0] ? options[0] : "#{prefix}:#{resource}"
|
77
|
+
ret += link_for("#{prefix}:#{resource}",title)
|
78
|
+
end
|
79
|
+
ret
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
def pipe_trick(page)
|
84
|
+
t = page.split(":")
|
85
|
+
t = t[1..-1] if t.size > 1
|
86
|
+
return t.join("").split(/[,(]/)[0]
|
87
|
+
end
|
88
|
+
|
89
|
+
# this code needs some work... lots of work
|
90
|
+
def wiki_image(resource,options)
|
91
|
+
pre_img = ''
|
92
|
+
post_img = ''
|
93
|
+
css = []
|
94
|
+
loc = "right"
|
95
|
+
type = nil
|
96
|
+
w = 180
|
97
|
+
h = nil
|
98
|
+
title = nil
|
99
|
+
ffloat = false
|
100
|
+
|
101
|
+
options.each do |x|
|
102
|
+
case
|
103
|
+
when ["thumb","thumbnail","frame","border"].include?(x.strip)
|
104
|
+
type = x.strip
|
105
|
+
when ["left","right","center","none"].include?(x.strip)
|
106
|
+
ffloat = true
|
107
|
+
loc = x.strip
|
108
|
+
when x.strip =~ /^([0-9]+)\s*px$/
|
109
|
+
w = $1
|
110
|
+
css << "width:#{w}px"
|
111
|
+
when x.strip =~ /^([0-9]+)\s*x\s*([0-9]+)\s*px$/
|
112
|
+
w = $1
|
113
|
+
css << "width:#{w}px"
|
114
|
+
h = $2
|
115
|
+
css << "height:#{h}px"
|
116
|
+
else
|
117
|
+
title = x.strip
|
118
|
+
end
|
119
|
+
end
|
120
|
+
css << "float:#{loc}" if ffloat == true
|
121
|
+
css << "border:1px solid #000" if type == "border"
|
122
|
+
|
123
|
+
sane_title = title.nil? ? "" : title.gsub(/<\/?[^>]*>/, "")
|
124
|
+
if type == "thumb" || type == "thumbnail" || type == "frame"
|
125
|
+
pre_img = '<div class="thumb t' + loc + '"><div class="thumbinner" style="width: ' + w.to_s +
|
126
|
+
'px;"><a href="" class="image" title="' + sane_title + '">'
|
127
|
+
post_img = '</a><div class="thumbcaption">' + title + '</div></div></div>'
|
128
|
+
end
|
129
|
+
"#{pre_img}<img src=\"#{resource}\" alt=\"#{sane_title}\" title=\"#{sane_title}\" style=\"#{css.join(";")}\" />#{post_img}"
|
130
|
+
end
|
131
|
+
|
132
|
+
def elem
|
133
|
+
Builder::XmlMarkup.new
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|