tartan 0.1.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.
Files changed (83) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +130 -0
  3. data/TODO +17 -0
  4. data/lib/core_ext/array.rb +12 -0
  5. data/lib/core_ext/file.rb +15 -0
  6. data/lib/core_ext/hash.rb +16 -0
  7. data/lib/core_ext/match_data.rb +62 -0
  8. data/lib/core_ext/module.rb +17 -0
  9. data/lib/core_ext/regexp.rb +33 -0
  10. data/lib/markdown.yml +499 -0
  11. data/lib/symbolize.rb +78 -0
  12. data/lib/table.yml +63 -0
  13. data/lib/tartan.rb +359 -0
  14. data/lib/tartan_markdown.rb +8 -0
  15. data/lib/tartan_markdown_def.rb +7 -0
  16. data/lib/tartan_table_def.rb +7 -0
  17. data/lib/tartan_test_base_def.rb +5 -0
  18. data/lib/tartan_wikilink_def.rb +14 -0
  19. data/lib/test_base.yml +18 -0
  20. data/lib/wiki-test.rb +94 -0
  21. data/lib/wiki_rule.rb +240 -0
  22. data/lib/wikilink.yml +18 -0
  23. data/test/MarkdownTest_1.0/Amps and angle encoding.html +17 -0
  24. data/test/MarkdownTest_1.0/Amps and angle encoding.text +21 -0
  25. data/test/MarkdownTest_1.0/Auto links.html +18 -0
  26. data/test/MarkdownTest_1.0/Auto links.text +13 -0
  27. data/test/MarkdownTest_1.0/Backslash codeescapes.html +68 -0
  28. data/test/MarkdownTest_1.0/Backslash codeescapes.text +68 -0
  29. data/test/MarkdownTest_1.0/Backslash simpleescapes.html +33 -0
  30. data/test/MarkdownTest_1.0/Backslash simpleescapes.text +33 -0
  31. data/test/MarkdownTest_1.0/Blockquotes with code blocks.html +15 -0
  32. data/test/MarkdownTest_1.0/Blockquotes with code blocks.text +11 -0
  33. data/test/MarkdownTest_1.0/Hard-wrapped paragraphs with list-like lines.html +8 -0
  34. data/test/MarkdownTest_1.0/Hard-wrapped paragraphs with list-like lines.text +8 -0
  35. data/test/MarkdownTest_1.0/Horizontal rules.html +71 -0
  36. data/test/MarkdownTest_1.0/Horizontal rules.text +67 -0
  37. data/test/MarkdownTest_1.0/Inline HTML (Advanced).html +14 -0
  38. data/test/MarkdownTest_1.0/Inline HTML (Advanced).text +14 -0
  39. data/test/MarkdownTest_1.0/Inline HTML (Simple).html +72 -0
  40. data/test/MarkdownTest_1.0/Inline HTML (Simple).text +69 -0
  41. data/test/MarkdownTest_1.0/Inline HTML comments.html +13 -0
  42. data/test/MarkdownTest_1.0/Inline HTML comments.text +13 -0
  43. data/test/MarkdownTest_1.0/Links, inline style.html +9 -0
  44. data/test/MarkdownTest_1.0/Links, inline style.text +9 -0
  45. data/test/MarkdownTest_1.0/Links, reference style.html +18 -0
  46. data/test/MarkdownTest_1.0/Links, reference style.text +31 -0
  47. data/test/MarkdownTest_1.0/Literal quotes in titles.html +3 -0
  48. data/test/MarkdownTest_1.0/Literal quotes in titles.text +7 -0
  49. data/test/MarkdownTest_1.0/Markdown Documentation - Basics.html +314 -0
  50. data/test/MarkdownTest_1.0/Markdown Documentation - Basics.text +306 -0
  51. data/test/MarkdownTest_1.0/Markdown Documentation - Syntax.html +942 -0
  52. data/test/MarkdownTest_1.0/Markdown Documentation - Syntax.text +888 -0
  53. data/test/MarkdownTest_1.0/Nested blockquotes.html +9 -0
  54. data/test/MarkdownTest_1.0/Nested blockquotes.text +5 -0
  55. data/test/MarkdownTest_1.0/Ordered and unordered lists.html +137 -0
  56. data/test/MarkdownTest_1.0/Ordered and unordered lists.text +122 -0
  57. data/test/MarkdownTest_1.0/Strong and em together.html +7 -0
  58. data/test/MarkdownTest_1.0/Strong and em together.text +7 -0
  59. data/test/MarkdownTest_1.0/Tabs.html +25 -0
  60. data/test/MarkdownTest_1.0/Tabs.text +21 -0
  61. data/test/MarkdownTest_1.0/Tidyness.html +8 -0
  62. data/test/MarkdownTest_1.0/Tidyness.text +5 -0
  63. data/test/MarkdownTest_1.0/run-markdown.rb +56 -0
  64. data/test/MarkdownTest_1.0/test-fireball-markdown.rb +177 -0
  65. data/test/MarkdownTest_1.0/testdiff.rb +42 -0
  66. data/test/harder/test-markdown-harder.rb +11 -0
  67. data/test/harder/test-markdown-harder.yml +111 -0
  68. data/test/redcloth/redcloth-markdown-tests.rb +29 -0
  69. data/test/redcloth/redcloth-markdown-tests.yml +218 -0
  70. data/test/test-combo.rb +23 -0
  71. data/test/test-hash.rb +31 -0
  72. data/test/test-markdown.rb +11 -0
  73. data/test/test-markdown.yml +1144 -0
  74. data/test/test-match-data.rb +54 -0
  75. data/test/test-readme-example.rb +48 -0
  76. data/test/test-tables.rb +16 -0
  77. data/test/test-tables.yml +82 -0
  78. data/test/test-tartan-markdown.rb +11 -0
  79. data/test/test-tartan.rb +306 -0
  80. data/test/test-wikilink.rb +18 -0
  81. data/test/test-wikilink.yml +22 -0
  82. data/test/wikilink-test-helper.rb +14 -0
  83. metadata +139 -0
@@ -0,0 +1,8 @@
1
+ #$Id: tartan_markdown.rb 148 2006-07-22 14:04:20Z larry $
2
+
3
+ require 'tartan'
4
+ require 'tartan_markdown_def'
5
+
6
+ class TartanMarkdown < Tartan
7
+ include TartanMarkdownDef
8
+ end
@@ -0,0 +1,7 @@
1
+ #$Id: tartan_markdown_def.rb 153 2006-07-22 20:39:45Z larry $
2
+
3
+ require 'tartan'
4
+
5
+ module TartanMarkdownDef
6
+ yaml 'markdown.yml'
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'tartan'
2
+
3
+ # This module should be included into a Tartan subclass to add table mark-up.
4
+
5
+ module TartanTableDef
6
+ yaml "table.yml"
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'tartan'
2
+
3
+ module TartanTestBaseDef
4
+ yaml "test_base.yml"
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'tartan'
2
+
3
+ module TartanWikilinkDef
4
+ def wiki_link_body(page_name, link_body)
5
+ link_body = page_name if "" == link_body
6
+ link_body
7
+ end
8
+
9
+ def wiki_link_tag(page_name)
10
+ raise "wiki_link_tag not properly defined in WikiHelper class"
11
+ end
12
+
13
+ yaml "wikilink.yml"
14
+ end
data/lib/test_base.yml ADDED
@@ -0,0 +1,18 @@
1
+ # $Id$
2
+ block:
3
+ - title: main_block
4
+ - title: html_comment
5
+ - title: paragraph
6
+ match: "/((?:^\S.*?$)(?:\n\S[^\n]*)*)/m"
7
+ subparse: span
8
+ html:
9
+ end_mark: "</p>"
10
+ start_mark: "<p>"
11
+ span:
12
+ - title: pre_emphasis
13
+ - title: amp
14
+ - title: italic
15
+ html:
16
+ replace: "<em>\\1</em>"
17
+ match: "/\*(.*?)\*/m"
18
+ - rescan
data/lib/wiki-test.rb ADDED
@@ -0,0 +1,94 @@
1
+ #$Id: wiki-test.rb 159 2006-07-24 08:13:02Z larry $
2
+
3
+ require 'test/unit'
4
+ require 'yaml'
5
+ require 'benchmark'
6
+ require 'breakpoint'
7
+ require 'core_ext/file'
8
+
9
+ class WikiTestCases
10
+ def initialize(parserKlass, testfiles, targets=[:html, :db])
11
+
12
+ # if either the testfile or target argument is a single value, turn
13
+ # it into a single element array
14
+ testfiles = testfiles.to_a
15
+
16
+ targests = targets.to_a
17
+
18
+ Class.new Test::Unit::TestCase do
19
+
20
+ define_method 'parserClass' do
21
+ parserKlass
22
+ end
23
+
24
+ def assert_wiki(expected, wiki_text, options, type=:html)
25
+ # time limit in seconds
26
+ time_limit = options['time_limit'] ? options['time_limit'] : 1.0
27
+
28
+ parser = parserClass.new(wiki_text, options)
29
+
30
+ out = nil
31
+ wtimes = Benchmark.measure do
32
+ out = parser.to_type(type)
33
+ end
34
+
35
+ time_elapsed = wtimes.utime + wtimes.stime
36
+
37
+ if $DEBUG || options['debug'] || options[:debug]
38
+ print "time_limit: #{'%0.3f' % time_limit}\n"+
39
+ "time_elapsed: #{'%0.3f' % time_elapsed}"
40
+ end
41
+
42
+ assert_equal(expected, out)
43
+ assert time_elapsed <= time_limit,
44
+ "elapsed time expected <= #{'%0.3f' % time_limit}"+
45
+ " but was #{'%0.3f' % time_elapsed}\n"
46
+ end
47
+
48
+ sequence = 0
49
+ globalOptions = (defined? WikiOptions) ? WikiOptions.dup : {}
50
+ globalSubs = {}
51
+ testfiles.each do |testfile|
52
+ file = File.find_file_in_path(testfile)
53
+
54
+ YAML::load_documents( File.open( file ) ) do |doc|
55
+ next unless doc
56
+
57
+ globalOptions = doc['options.global'] if doc['options.global']
58
+
59
+ options = globalOptions
60
+
61
+ options = options.merge(doc['options']) if doc['options']
62
+
63
+ globalSubs = doc['subs.global'] if doc['subs.global']
64
+
65
+ subs = globalSubs
66
+
67
+ subs = subs.merge(options)
68
+
69
+ subs = subs.merge(doc['subs']) if doc['subs']
70
+
71
+ next unless doc['in']
72
+
73
+ input = doc['in'].gsub(/\{\{\{(.*?)\}\}\}/) {|sub| subs[$1]}
74
+
75
+ title = doc['title'] ? doc['title'] : (sequence += 1).to_s
76
+
77
+ targets.each do |target|
78
+ target = target.to_sym if target.is_a? String
79
+ target_name = target.id2name if target.is_a? Symbol
80
+
81
+ next unless doc[target_name]
82
+
83
+ expected \
84
+ = doc[target_name].gsub(/\{\{\{(.*?)\}\}\}/) {|sub| subs[$1]}
85
+
86
+ define_method "test_#{target_name}_" + title do
87
+ assert_wiki expected, input, options.dup, target
88
+ end # define_method
89
+ end # targests.each
90
+ end # YAML::load_documents
91
+ end # testfiles.each
92
+ end # Class.new
93
+ end # intialize
94
+ end # class WikiTestCases
data/lib/wiki_rule.rb ADDED
@@ -0,0 +1,240 @@
1
+ # $Id: wiki_rule.rb 159 2006-07-24 08:13:02Z larry $
2
+ require 'core_ext/regexp'
3
+
4
+ class WikiRule < Hash
5
+ def initialize(matcher={})
6
+ matcher.each { |(a, v)| self[a] = v }
7
+ end
8
+
9
+ def matcher
10
+ self
11
+ end
12
+
13
+ # define methods to access elements of matcher hash
14
+ def method_missing(method_symbol)
15
+ self.class.send(:define_method, method_symbol) {self[method_symbol]}
16
+ self[method_symbol]
17
+ end
18
+
19
+ def match
20
+ self[:match] ? self[:match].to_regexp : nil
21
+ end
22
+
23
+ def strip_match
24
+ strip = self[:strip]
25
+ (strip and strip[:match]) ? strip[:match].to_regexp : nil
26
+ end
27
+
28
+ def unstrip_match
29
+ unstrip = self[:unstrip]
30
+ (unstrip and unstrip[:match]) ? unstrip[:match].to_regexp : nil
31
+ end
32
+
33
+ def strip_replace
34
+ self[:strip][:replace]
35
+ end
36
+
37
+ def unstrip_replace
38
+ self[:unstrip][:replace]
39
+ end
40
+
41
+ def placed(position)
42
+ self.delete(position)
43
+ end
44
+
45
+ def default_rule
46
+ self[:default]
47
+ end
48
+
49
+ def start_mark(type)
50
+ return false if self[type].nil? or self[type].class == TrueClass
51
+
52
+ self[type][:start_mark]
53
+ end
54
+
55
+ def end_mark(type)
56
+ return false if self[type].nil? or self[type].class == TrueClass
57
+
58
+ self[type][:end_mark]
59
+ end
60
+
61
+ def replace_regex(type)
62
+ return false if self[type].nil? or self[type].class == TrueClass
63
+
64
+ self[type][:replace]
65
+ end
66
+
67
+ def target_position
68
+ self[:position]
69
+ end
70
+
71
+ def vars_to_set
72
+ self[:set]
73
+ end
74
+
75
+ def subparse_context
76
+ return nil if ! self.has_key?(:subparse)
77
+
78
+ if self[:subparse].is_a?(Symbol) or self[:subparse].is_a?(String)
79
+ return self[:subparse]
80
+ elsif self[:subparse].has_key?(:context)
81
+ return self[:subparse][:context]
82
+ end
83
+
84
+ return nil
85
+ end
86
+
87
+ def subparse_match_group
88
+ if has_key?(:subparse) and
89
+ self[:subparse].is_a?(Hash) and
90
+ self[:subparse].has_key?(:match_group)
91
+ self[:subparse][:match_group]
92
+ else
93
+ nil
94
+ end
95
+ end
96
+
97
+ def position_matches?(parsed, to_be_parsed, match_result)
98
+ return true if !target_position
99
+
100
+ pos = parsed.length
101
+
102
+ return true if target_position == pos
103
+ return true if :first == target_position && 0 == pos
104
+
105
+ if 0 == to_be_parsed.length && match_result.post_match.empty?
106
+ return true if :last == target_position
107
+ return true if :all == target_position && 0 == parsed.length
108
+ else
109
+ return true if :not_last == target_position
110
+ end
111
+
112
+ return false
113
+ end
114
+
115
+ def has_type?(type)
116
+ if String == type.class
117
+ type = type.to_sym
118
+ end
119
+
120
+ return self[type]
121
+ end
122
+
123
+ def replace(parse_type, match_result, wikiparser)
124
+ return unless match_result
125
+
126
+ wikiparser.substitute(replace_regex(parse_type), match_result)
127
+ end
128
+
129
+ def block(parse_type, text, match_result, wikiparser)
130
+ return unless match_result
131
+
132
+ mid = text
133
+
134
+ start_strip = ""
135
+ end_strip = ""
136
+
137
+ if subparse_match_group
138
+ subparse_index = match_result.sym2index(subparse_match_group)
139
+
140
+ last_match = match_result.last_match_index
141
+ start_strip = match_result.pre_sub_match(subparse_index)
142
+ end_strip = match_result.post_sub_match(subparse_index)
143
+ mid = match_result[subparse_index]
144
+ end
145
+
146
+ if strip.is_a?(Hash) and strip[:match] and strip[:replace]
147
+ mid = mid.gsub(strip_match, strip_replace)
148
+ end
149
+
150
+ # need to evaluate the start_text before the execution of the
151
+ # sub-parse in case any of the wiki helper functions depend on
152
+ # ordering of the text
153
+ start_text = start_mark(parse_type)?
154
+ wikiparser.substitute(start_mark(parse_type),
155
+ match_result):
156
+ ""
157
+ if subparse_context
158
+ mid = wikiparser.parse_by_context(parse_type, subparse_context, mid)
159
+ end
160
+
161
+ mid = start_text + mid
162
+
163
+ if end_mark(parse_type)
164
+ mid += wikiparser.substitute(end_mark(parse_type), match_result)
165
+ end
166
+
167
+ if ! has_key?(parse_type)
168
+ mid = start_strip + mid + end_strip
169
+
170
+ if unstrip
171
+ mid.gsub!(unstrip_match, unstrip_replace)
172
+ end
173
+ end
174
+
175
+ mid
176
+ end
177
+
178
+ def handle_match(parse_type, parsed, to_be_parsed, match_result, wikiparser)
179
+ debug_opt = wikiparser.get_var('debug')
180
+ if $DEBUG || debug || true == debug_opt || debug_opt == title
181
+ puts "\n%s matches [[%s<<\n%s\n>>%s]]" %
182
+ [title, match_result.pre_match, match_result,
183
+ match_result.post_match]
184
+ end
185
+
186
+ wikiparser.set_vars(vars_to_set, match_result)
187
+
188
+ mid = match_result[0]
189
+
190
+ if replace_regex(parse_type)
191
+ mid = replace(parse_type, match_result, wikiparser)
192
+ elsif true == self[parse_type] or
193
+ start_mark(parse_type) or
194
+ end_mark(parse_type) or
195
+ subparse
196
+
197
+ mid = block(parse_type, mid, match_result, wikiparser)
198
+ end
199
+
200
+ if shelve && mid
201
+ mid = wikiparser.shelve(mid)
202
+ end
203
+
204
+ if "" != match_result.pre_match
205
+ parsed.add_text(false, match_result.pre_match)
206
+ end
207
+
208
+ parsed.add_text(true, mid) if "" != mid
209
+
210
+ if "" != match_result.post_match
211
+ to_be_parsed.add_back_text(false, match_result.post_match)
212
+ end
213
+
214
+ return parsed, to_be_parsed
215
+ end
216
+
217
+ def parse_element(parse_type, to_be_parsed, wikiparser)
218
+ parsed = ParsedTextList.new
219
+
220
+ position = 0
221
+ while element = to_be_parsed.shift
222
+ if element.claimed? then parsed.add(element); next ; end
223
+
224
+ match_result = match.match(element)
225
+
226
+ unless match_result then parsed.add(element); next; end
227
+
228
+ if !position_matches?(parsed, to_be_parsed, match_result)
229
+ parsed.add(element)
230
+ next
231
+ end
232
+
233
+ parsed, to_be_parsed = handle_match(parse_type, parsed,
234
+ to_be_parsed,
235
+ match_result, wikiparser)
236
+ end
237
+
238
+ parsed
239
+ end
240
+ end
data/lib/wikilink.yml ADDED
@@ -0,0 +1,18 @@
1
+ span:
2
+ - title: wikilinkWithText
3
+ beginning: true
4
+ match: "/\[\[([^|]+)[|]([^\]]*)\]\]/"
5
+ shelve: true
6
+ subparse:
7
+ context: span
8
+ match_group: 2
9
+ html:
10
+ start_mark: "#{wiki_link_tag(\1)}"
11
+ end_mark: </a>
12
+
13
+ - title: wikilink
14
+ after: wikilinkWithText
15
+ match: "/\[\[([^\]]+)\]\]/"
16
+ shelve: true
17
+ html:
18
+ replace: "#{wiki_link_tag(\1)}\1</a>"
@@ -0,0 +1,17 @@
1
+ <p>AT&amp;T has an ampersand in their name.</p>
2
+
3
+ <p>AT&amp;T is another way to write it.</p>
4
+
5
+ <p>This &amp; that.</p>
6
+
7
+ <p>4 &lt; 5.</p>
8
+
9
+ <p>6 > 5.</p>
10
+
11
+ <p>Here's a <a href="http://example.com/?foo=1&amp;bar=2">link</a> with an ampersand in the URL.</p>
12
+
13
+ <p>Here's a link with an amersand in the link text: <a href="http://att.com/" title="AT&amp;T">AT&amp;T</a>.</p>
14
+
15
+ <p>Here's an inline <a href="/script?foo=1&amp;bar=2">link</a>.</p>
16
+
17
+ <p>Here's an inline <a href="/script?foo=1&amp;bar=2">link</a>.</p>
@@ -0,0 +1,21 @@
1
+ AT&T has an ampersand in their name.
2
+
3
+ AT&amp;T is another way to write it.
4
+
5
+ This & that.
6
+
7
+ 4 < 5.
8
+
9
+ 6 > 5.
10
+
11
+ Here's a [link] [1] with an ampersand in the URL.
12
+
13
+ Here's a link with an amersand in the link text: [AT&T] [2].
14
+
15
+ Here's an inline [link](/script?foo=1&bar=2).
16
+
17
+ Here's an inline [link](</script?foo=1&bar=2>).
18
+
19
+
20
+ [1]: http://example.com/?foo=1&bar=2
21
+ [2]: http://att.com/ "AT&T"