wikicloth 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -25,7 +25,7 @@ Install
25
25
  git clone git://github.com/nricciar/wikicloth.git
26
26
  cd wikicloth/
27
27
  gem build wikicloth.gemspec
28
- sudo gem install wikicloth-0.1.3.gem
28
+ sudo gem install wikicloth-0.1.6.gem
29
29
 
30
30
  Usage
31
31
  ---------------------------------------------------
data/Rakefile CHANGED
@@ -25,7 +25,7 @@ end
25
25
 
26
26
  spec = Gem::Specification.new do |s|
27
27
  s.name = "wikicloth"
28
- s.version = %q{0.1.5}
28
+ s.version = %q{0.1.6}
29
29
  s.author = "David Ricciardi"
30
30
  s.email = "nricciar@gmail.com"
31
31
  s.homepage = "http://github.com/nricciar/wikicloth"
@@ -21,6 +21,12 @@ module WikiCloth
21
21
  end
22
22
  end
23
23
 
24
+ def toc(&block)
25
+ self.send :define_method, 'toc' do |sections|
26
+ block.call(sections)
27
+ end
28
+ end
29
+
24
30
  def external_link(&block)
25
31
  self.send :define_method, 'external_link' do |url,text|
26
32
  block.call(url,text)
@@ -41,6 +47,12 @@ module WikiCloth
41
47
  end
42
48
  end
43
49
 
50
+ def section_link(&block)
51
+ self.send :define_method, 'section_link' do |section|
52
+ block.call(section)
53
+ end
54
+ end
55
+
44
56
  def template(&block)
45
57
  self.send :define_method, 'template' do |template|
46
58
  block.call(template)
@@ -7,8 +7,8 @@ class WikiBuffer::HTMLElement < WikiBuffer
7
7
 
8
8
  ALLOWED_ELEMENTS = ['a','b','i','div','span','sup','sub','strike','s','u','font','big','ref','tt','del',
9
9
  'small','blockquote','strong','pre','code','references','ol','li','ul','dd','dt','dl','center',
10
- 'h1','h2','h3','h4','h5','h6','p']
11
- ALLOWED_ATTRIBUTES = ['id','name','style','class','href','start','value']
10
+ 'h1','h2','h3','h4','h5','h6','p','table','tr','td','th','tbody','thead','tfoot']
11
+ ALLOWED_ATTRIBUTES = ['id','name','style','class','href','start','value','colspan']
12
12
  ESCAPED_TAGS = [ 'nowiki', 'pre', 'code' ]
13
13
  SHORT_TAGS = [ 'meta','br','hr','img' ]
14
14
  NO_NEED_TO_CLOSE = ['li','p'] + SHORT_TAGS
@@ -9,6 +9,7 @@ class WikiBuffer::Table < WikiBuffer
9
9
  @start_row = false
10
10
  @start_caption = false
11
11
  @in_quotes = false
12
+ @attribute_name = nil
12
13
  end
13
14
 
14
15
  def table_caption
@@ -31,12 +32,14 @@ class WikiBuffer::Table < WikiBuffer
31
32
  ">#{table_caption.strip}</caption>" unless self.table_caption.blank?
32
33
  for row in rows
33
34
  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]}>"
35
+ unless row.empty?
36
+ ret += "<tr" + (params[row_count].nil? || params[row_count].blank? ? "" : " #{params[row_count].strip}") + ">"
37
+ for cell in row
38
+ cell_attributes = cell[:style].blank? ? "" : " #{cell[:style].strip}"
39
+ ret += "<#{cell[:type]}#{cell_attributes}> #{cell[:value].strip}\n</#{cell[:type]}>"
40
+ end
41
+ ret += "</tr>"
38
42
  end
39
- ret += "</tr>"
40
43
  end
41
44
  ret += "</table>"
42
45
  end
@@ -84,6 +87,10 @@ class WikiBuffer::Table < WikiBuffer
84
87
  end
85
88
 
86
89
  case
90
+ when @check_cell_data == 1 && current_char == "="
91
+ @attribute_name = self.data.chop!
92
+ self.data = ""
93
+
87
94
  # Next table cell in row (TD)
88
95
  when current_char == "|" && (previous_char == "\n" || previous_char == "|") && @in_quotes == false
89
96
  self.data.chop!
@@ -122,6 +129,9 @@ class WikiBuffer::Table < WikiBuffer
122
129
  when current_char == '"' && previous_char != '\\'
123
130
  @in_quotes = !@in_quotes
124
131
  self.data += '"'
132
+ if @attribute_name && @in_quotes == false
133
+ puts "ATTR #{@attribute_name} = #{self.data}"
134
+ end
125
135
 
126
136
  # Table params
127
137
  when current_char == "\n" && @start_table == true && @in_quotes == false
@@ -117,20 +117,10 @@ class WikiBuffer
117
117
  self.data.gsub!(/---([^-]+)---/,"<strike>\\1</strike>")
118
118
  self.data.gsub!(/_([^_]+)_/,"<u>\\1</u>")
119
119
  end
120
- self.data.gsub!(/__([a-zA-Z0-9]+)__/) { |r|
121
- case $1
122
- when "NOEDITSECTION"
123
- @noeditsection = true
124
- end
125
- ""
126
- }
120
+ self.data.gsub!(/__TOC__/) { |r| @options[:link_handler].toc(@options[:sections]) }
127
121
  self.data.gsub!(/^([-]{4,})/) { |r| "<hr />" }
128
122
  self.data.gsub!(/^([=]{1,6})\s*(.*?)\s*(\1)/) { |r|
129
- @section_count += 1
130
- "<h#{$1.length}>" + (@noeditsection == true ? "" :
131
- "<span class=\"editsection\">[<a href=\"" + @options[:link_handler].section_link(@section_count) +
132
- "\" title=\"Edit section: #{$2}\">edit</a>]</span>") +
133
- " <span class=\"mw-headline\">#{$2}</span></h#{$1.length}>"
123
+ "<h#{$1.length}>#{$2}</h#{$1.length}>"
134
124
  }
135
125
  self.data.gsub!(/([\']{2,5})(.*?)(\1)/) { |r|
136
126
  tmp = "<i>#{$2}</i>" if $1.length == 2
@@ -10,13 +10,38 @@ class WikiLinkHandler
10
10
  end
11
11
 
12
12
  def section_link(section)
13
- ""
13
+ "?section=#{section}"
14
14
  end
15
15
 
16
16
  def params
17
17
  @params ||= {}
18
18
  end
19
19
 
20
+ def toc(sections)
21
+ parent = sections.first
22
+ nest_depth = 0
23
+
24
+ ret = "<div style=\"font-weight:bold\">Table of Contents</div><ul>"
25
+ for section in sections[1..-1]
26
+ sid = section[:id].split("-").first
27
+ prev_sid = sid if prev_sid.nil?
28
+
29
+ if sid.split(".").length > prev_sid.split(".").length
30
+ ret += "<ul>"
31
+ nest_depth += 1
32
+ end
33
+ if sid.split(".").length < prev_sid.split(".").length
34
+ ret += "</ul>"
35
+ nest_depth -= 1
36
+ end
37
+ ret += "<li><a href=\"##{section[:id]}\">#{section[:title]}</a></li>"
38
+ prev_sid = sid
39
+ end
40
+ ret += "</ul>"
41
+ nest_depth.times { ret += "</ul>" }
42
+ ret
43
+ end
44
+
20
45
  def template(template)
21
46
  nil
22
47
  end
data/lib/wikicloth.rb CHANGED
@@ -15,12 +15,61 @@ module WikiCloth
15
15
  end
16
16
 
17
17
  def load(data,p={})
18
- data.gsub!(/<!--(.|\s)*?-->/,"")
19
- data = data.gsub(/^[^\s]*\{\{(.*?)\}\}/){ |match| expand_templates($1,["."]) }
18
+ self.sections = get_sections(data)
20
19
  self.params = p
20
+ self.sections.first[:content] += "__TOC__" unless data =~ /__NOTOC__|__TOC__/
21
+ data = self.sections.collect { |s| s[:heading]+s[:content] }.join("")
22
+ data.gsub!(/<!--(.|\s)*?-->/,"")
23
+ data.gsub!(/^[^\s]*\{\{(.*?)\}\}/){ |match| expand_templates($1,["."]) }
21
24
  self.html = data
22
25
  end
23
26
 
27
+ def sections=(val)
28
+ @sections = val
29
+ end
30
+
31
+ def sections
32
+ @sections
33
+ end
34
+
35
+ def get_sections(data)
36
+ last_head = "1"
37
+ noedit = false
38
+ sections = [{ :title => "", :content => "", :id => "1", :heading => "" }]
39
+
40
+ for line in data
41
+ if line =~ /^([=]{1,6})\s*(.*?)\s*(\1)/
42
+ sections << { :title => $2, :content => "", :heading => "", :id => "" }
43
+
44
+ section_depth = $1.length
45
+ section_title = $2
46
+
47
+ if last_head.nil?
48
+ last_head = "#{section_depth}"
49
+ else
50
+ tmp = last_head.split(".")
51
+ if tmp.last.to_i < section_depth
52
+ last_head = "#{tmp[0..-1].join(".")}.#{section_depth}"
53
+ elsif tmp.last.to_i == section_depth
54
+ last_head = "#{tmp[0..-1].join(".")}"
55
+ else
56
+ last_head = "#{tmp[0..-2].join(".")}"
57
+ end
58
+ end
59
+ sections.last[:id] = get_id_for(last_head)
60
+ sections.last[:heading] = "<h#{section_depth}>" + (noedit == true ? "" :
61
+ "<span class=\"editsection\">&#91;<a href=\"" + self.link_handler.section_link(sections.last[:id]) +
62
+ "\" title=\"Edit section: #{section_title}\">edit</a>&#93;</span>") +
63
+ " <span id=\"#{sections.last[:id]}\" class=\"mw-headline\">#{section_title}</span></h#{section_depth}>"
64
+ elsif line =~ /__NOEDITSECTION__/
65
+ noedit = true
66
+ else
67
+ sections.last[:content] += "#{line}\n"
68
+ end
69
+ end
70
+ sections
71
+ end
72
+
24
73
  def expand_templates(template, stack)
25
74
  template.strip!
26
75
  article = link_handler.template(template)
@@ -31,7 +80,7 @@ module WikiCloth
31
80
  unless stack.include?(template)
32
81
  data = article
33
82
  else
34
- data = "template loop! OHNOES!"
83
+ data = "WARNING: TEMPLATE LOOP"
35
84
  end
36
85
  data = data.gsub(/^[^\s]*\{\{(.*?)\}\}/){ |match| expand_templates($1,stack + [template])}
37
86
  end
@@ -40,7 +89,7 @@ module WikiCloth
40
89
  end
41
90
 
42
91
  def render(opt={})
43
- self.options = { :output => :html, :link_handler => self.link_handler, :params => self.params }.merge(opt)
92
+ self.options = { :output => :html, :link_handler => self.link_handler, :params => self.params, :sections => self.sections }.merge(opt)
44
93
  self.options[:link_handler].params = options[:params]
45
94
  buffer = WikiBuffer.new("",options)
46
95
  self.html.each_char { |c| buffer.add_char(c) }
@@ -64,6 +113,13 @@ module WikiCloth
64
113
  end
65
114
 
66
115
  protected
116
+ def get_id_for(val)
117
+ @idmap ||= {}
118
+ @idmap[val] ||= 0
119
+ @idmap[val] += 1
120
+ "#{val}-#{@idmap[val]}"
121
+ end
122
+
67
123
  def options=(val)
68
124
  @options = val
69
125
  end
@@ -0,0 +1,76 @@
1
+ {| style='width: 35em; margin: 1em auto; border-spacing: 0; text-align: center'
2
+ |-
3
+ | colspan='4' |
4
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0.2em; border-bottom: 3px solid #999; background: #eeeeee' | 0
5
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0' | .
6
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0.2em; border-bottom: 3px solid #9df; background: #eef5ff' | 0
7
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0' | .
8
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0.2em; border-bottom: 3px solid #db8; background: #fff5ee' | 17
9
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0' | .
10
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0.2em; border-bottom: 3px solid #bd8; background: #f5ffee' | 0
11
+ | colspan='1' width='1%' style='font-size: 200%; padding: 0' | .
12
+ | colspan='2' width='1%' style='font-size: 200%; padding: 0.2em; border-bottom: 3px solid #999; background: #eeeeee' | 246
13
+ | colspan='4' |
14
+ |-
15
+ | style='height: 1em' |
16
+ | style='border-bottom: 1px solid #999' |
17
+ | style='width: 1em; border-bottom: 1px solid #999' |
18
+ | style='width: 15em' style='border-bottom: 1px solid #999' |
19
+ | width='1%' style='border-right: 1px solid #999; border-bottom: 1px solid #999' |
20
+ | width='1%' style='border-left: 1px solid #999' |
21
+ | width='1%' style='border-bottom: 1px solid #9df' |
22
+ | width='1%' style='border-bottom: 1px solid #9df' |
23
+ | width='1%' style='border-right: 1px solid #9df; border-bottom: 1px solid #9df' |
24
+ | width='1%' style='border-left: 1px solid #9df' |
25
+ ||
26
+ ||
27
+ | width='1%' style='border-right: 1px solid #db8' |
28
+ | width='1%' style='border-left: 1px solid #db8' |
29
+ ||
30
+ ||
31
+ | width='1%' style='border-right: 1px solid #bd8' |
32
+ | width='1%' style='border-left: 1px solid #bd8; border-bottom: 1px solid #bd8' |
33
+ | width='1%' style='border-bottom: 1px solid #bd8' |
34
+ | style='border-right: 1px solid #999' |
35
+ | width='1%' style='border-bottom: 1px solid #999; border-left: 1px solid #999' |
36
+ | style='width: 3em; border-bottom: 1px solid #999'|
37
+ | style='width: 3em; border-bottom: 1px solid #999' |
38
+ | style='border-bottom: 1px solid #999' |
39
+ | style='height: 1em' |
40
+ |-
41
+ | style='height: 1em; border-right: 1px solid #999' |
42
+ | style='border-left: 1px solid #999; border-top: 1px solid #999' |
43
+ | style='border-top: 1px solid #999' |
44
+ | style='border-top: 1px solid #999' |
45
+ | style='border-top: 1px solid #999' |
46
+ | style='border-right: 1px solid #9df' |
47
+ | style='border-left: 1px solid #9df; border-top: 1px solid #9df' |
48
+ | style='border-top: 1px solid #9df' |
49
+ | width='1%' style='border-top: 1px solid #9df' |
50
+ | width='1%' |
51
+ ||
52
+ ||
53
+ | width='1%' style='border-right: 1px solid #db8' |
54
+ | width='1%' style='border-left: 1px solid #db8' |
55
+ ||
56
+ ||
57
+ ||
58
+ | width='1%' style='border-top: 1px solid #bd8' |
59
+ | width='1%' style='border-right: 1px solid #bd8; border-top: 1px solid #bd8' |
60
+ | width='1%' style='border-left: 1px solid #bd8' |
61
+ | width='1%' style='border-top: 1px solid #999' |
62
+ | style='border-top: 1px solid #999' |
63
+ | style='border-top: 1px solid #999' |
64
+ | style='border-right: 1px solid #999; border-top: 1px solid #999' |
65
+ | width='40' style='border-left: 1px solid #999' |
66
+ |-
67
+ | colspan='2' style='padding: 0.25em; border: 2px solid #999; background: #eeeeee' | Major version number.
68
+ | colspan='1' | <div style='width: 1.5em'></div>
69
+ | colspan='5' style='padding: 0.25em; border: 2px solid #9df; background: #eef5ff' | Level of magma (0-6/7), new major version when it reaches 7.
70
+ | colspan='1' | <div style='width: 0.5em'></div>
71
+ | colspan='8' style='padding: 0.25em; border: 2px solid #db8; background: #fff5ee' | Minor feature version.
72
+ | colspan='1' | <div style='width: 0.5em'></div>
73
+ | colspan='4' style='padding: 0.25em; border: 2px solid #bd8; background: #f5ffee' | Bug/security fixes on current "major.magma.minor" version combo.
74
+ | colspan='1' | <div style='width: 1.5em'></div>
75
+ | colspan='2' style='padding: 0.25em; border: 2px solid #999; background: #eeeeee' | Number of expletives uttered during development.
76
+ |}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikicloth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Ricciardi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-10 00:00:00 -04:00
18
+ date: 2010-09-04 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,32 +29,33 @@ extra_rdoc_files:
29
29
  - README
30
30
  - MIT-LICENSE
31
31
  files:
32
- - lib/wikicloth.rb
33
- - lib/wikicloth/wiki_buffer.rb
34
- - lib/wikicloth/parser.rb
35
32
  - lib/wikicloth/core_ext.rb
36
- - lib/wikicloth/wiki_buffer/table.rb
33
+ - lib/wikicloth/parser.rb
37
34
  - lib/wikicloth/wiki_buffer/html_element.rb
38
- - lib/wikicloth/wiki_buffer/var.rb
39
35
  - lib/wikicloth/wiki_buffer/link.rb
36
+ - lib/wikicloth/wiki_buffer/table.rb
37
+ - lib/wikicloth/wiki_buffer/var.rb
38
+ - lib/wikicloth/wiki_buffer.rb
40
39
  - lib/wikicloth/wiki_link_handler.rb
40
+ - lib/wikicloth.rb
41
41
  - tasks/wikicloth_tasks.rake
42
+ - sample_documents/air_force_one.wiki
43
+ - sample_documents/cheatsheet.wiki
44
+ - sample_documents/elements.wiki
42
45
  - sample_documents/george_washington.wiki
46
+ - sample_documents/images.wiki
47
+ - sample_documents/lists.wiki
43
48
  - sample_documents/pipe_trick.wiki
44
49
  - sample_documents/random.wiki
45
50
  - sample_documents/tv.wiki
46
- - sample_documents/air_force_one.wiki
47
- - sample_documents/lists.wiki
48
- - sample_documents/images.wiki
49
- - sample_documents/elements.wiki
51
+ - sample_documents/versioning.wiki
50
52
  - sample_documents/wiki_tables.wiki
51
- - sample_documents/cheatsheet.wiki
52
53
  - init.rb
53
54
  - uninstall.rb
54
55
  - Rakefile
55
56
  - install.rb
56
- - test/wiki_cloth_test.rb
57
57
  - test/test_helper.rb
58
+ - test/wiki_cloth_test.rb
58
59
  - run_tests.rb
59
60
  - README
60
61
  - MIT-LICENSE
@@ -93,6 +94,6 @@ signing_key:
93
94
  specification_version: 3
94
95
  summary: An implementation of the mediawiki markup in ruby
95
96
  test_files:
96
- - test/wiki_cloth_test.rb
97
97
  - test/test_helper.rb
98
+ - test/wiki_cloth_test.rb
98
99
  - run_tests.rb