trac-wiki 0.1.9 → 0.1.12

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c3c0e79552d6d60ecbb77c54fbaa434d208ca56
4
+ data.tar.gz: dd2e9ab2b74410c6b3abf6265d43185fcaec19cd
5
+ SHA512:
6
+ metadata.gz: c173fe3e96fdceaa1e587c2a41e75e50fa731bfb3403270463d3352014e8223dd459ba7df0e5ea9d6377be54d701d19ec3a00dfb3b259583cd70325ab9d5bc15
7
+ data.tar.gz: b7c6a4d413d44ea1bb30d9e49454420a5ecd639601ea79ef19f80a73e1831c1165bac4d94b9da75757d5ca440fc301ff5a062554329293f8bb5821e41a296e0f
data/Gemfile CHANGED
@@ -1,2 +1,3 @@
1
1
  source 'https://rubygems.org/'
2
2
  gemspec
3
+ gem 'iconv'
data/lib/trac-wiki.rb CHANGED
@@ -13,4 +13,7 @@ module TracWiki
13
13
  def self.render(text, options = {})
14
14
  Parser.new(text, options).to_html
15
15
  end
16
+ def self.parser(text, options = {})
17
+ Parser.new(text, options)
18
+ end
16
19
  end
@@ -1,5 +1,6 @@
1
1
  require 'cgi'
2
2
  require 'uri'
3
+ require 'iconv'
3
4
 
4
5
  # :main: TracWiki
5
6
 
@@ -43,6 +44,8 @@ module TracWiki
43
44
  # Examples: http https ftp ftps
44
45
  attr_accessor :allowed_schemes
45
46
 
47
+ attr_accessor :headings
48
+
46
49
 
47
50
  # Disable url escaping for local links
48
51
  # Escaping: [[/Test]] --> %2FTest
@@ -58,12 +61,30 @@ module TracWiki
58
61
  attr_writer :math
59
62
  def math?; @math; end
60
63
 
64
+ attr_writer :edit_heading
65
+ def edit_heading?; @edit_heading; end
66
+
67
+ # understand merge tags (see diff3(1))
68
+ # >>>>>>> mine
69
+ # ||||||| orig
70
+ # =======
71
+ # <<<<<<< yours
72
+ # convert to <div class="merge merge-mine">mine</div>
61
73
  attr_writer :merge
62
74
  def merge?; @merge; end
63
75
 
76
+ # every heading will had id, generated from heading text
77
+ attr_writer :id_from_heading
78
+ def id_from_heading?; @id_from_heading; end
79
+
80
+ # when id_from_heading, non ascii char are transliterated to ascii
81
+ attr_writer :id_translit
82
+ def id_translit?; @id_translit; end
83
+
64
84
  # Create a new Parser instance.
65
85
  def initialize(text, options = {})
66
86
  @allowed_schemes = %w(http https ftp ftps)
87
+ @anames = {}
67
88
  @text = text
68
89
  @no_escape = nil
69
90
  options.each_pair {|k,v| send("#{k}=", v) }
@@ -83,20 +104,33 @@ module TracWiki
83
104
  # #=> "<p><strong>Hello <em>World</em></strong></p>"
84
105
  def to_html
85
106
  @out = ''
107
+ @edit_heading_class = 'editheading'
108
+ @headings = [ {level: 0, sline: 1 } ]
86
109
  @p = false
87
110
  @stack = []
88
111
  @stacki = []
89
112
  @was_math = false
113
+ @line_no = 1
90
114
  parse_block(@text)
91
115
  @out
92
116
  end
93
117
 
118
+ def make_toc_html
119
+ @out = ''
120
+ parse_block(make_toc)
121
+ end
122
+
94
123
  protected
95
124
 
96
125
  # Escape any characters with special meaning in HTML using HTML
97
- # entities.
126
+ # entities. (&<>" not ')
98
127
  def escape_html(string)
99
- CGI::escapeHTML(string)
128
+ #CGI::escapeHTML(string)
129
+ Parser.escapeHTML(string)
130
+ end
131
+
132
+ def self.escapeHTML(string)
133
+ string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
100
134
  end
101
135
 
102
136
  # Escape any characters with special meaning in URLs using URL
@@ -176,7 +210,10 @@ module TracWiki
176
210
  # make_local_link("LocalLink") #=> "/LocalLink"
177
211
  # make_local_link("Wikipedia:Bread") #=> "http://en.wikipedia.org/wiki/Bread"
178
212
  def make_local_link(link) #:doc:
179
- no_escape? ? link : escape_url(link)
213
+ return link if no_escape?
214
+ link, anch = link.split(/#/, 2)
215
+ return escape_url(link) if ! anch
216
+ "#{escape_url(link)}##{escape_url(anch)}"
180
217
  end
181
218
 
182
219
  # Sanatize a direct url (e.g. http://wikipedia.org/). The default
@@ -246,13 +283,24 @@ module TracWiki
246
283
  end
247
284
 
248
285
  def make_headline(level, text, aname)
249
- ret = "<h#{level}>" << escape_html(text) << "</h#{level}>"
286
+ ret = "<h#{level}"
250
287
  if aname
251
- ret = "<a name=\"#{ escape_html(aname) }\"/>" + ret
288
+ ret += " id=\"#{ escape_html(aname) }\""
289
+ end
290
+ ret += ">" + escape_html(text)
291
+
292
+ if edit_heading?
293
+ ret += edit_heading_link(@headings.size - 1)
252
294
  end
295
+
296
+ ret += "</h#{level}>"
253
297
  ret
254
298
  end
255
299
 
300
+ def edit_heading_link(section)
301
+ "<a class='#{@edit_heading_class}' href=\"?edit=#{section}\">edit</a>"
302
+ end
303
+
256
304
  def make_explicit_link(link)
257
305
  begin
258
306
  uri = URI.parse(link)
@@ -262,6 +310,18 @@ module TracWiki
262
310
  make_local_link(link)
263
311
  end
264
312
 
313
+
314
+ def make_toc
315
+ @headings.map do |h|
316
+ if h[:level] < 1
317
+ ''
318
+ else
319
+ ind = " " * (h[:level] - 1)
320
+ "#{ind}* [[##{h[:aname]}|#{h[:title]}]]\n"
321
+ end
322
+ end.join
323
+ end
324
+
265
325
  def parse_inline(str)
266
326
  until str.empty?
267
327
  case str
@@ -512,10 +572,12 @@ module TracWiki
512
572
 
513
573
  # heading == Wiki Ruless ==
514
574
  # heading == Wiki Ruless == #tag
515
- when str =~ /\A\s*(={1,6})\s*(.*?)\s*=*\s*(#(\S*))?\s*$(\r?\n)?/
575
+ when str =~ /\A[[:blank:]]*(={1,6})\s*(.*?)\s*=*\s*(#(\S*))?\s*$(\r?\n)?/
516
576
  level = $1.size
517
577
  title= $2
518
- aname= $4
578
+ aname= aname_nice($4, title)
579
+ @headings.last[:eline] = @line_no - 1
580
+ @headings.push({ :title => title, :sline => @line_no, :aname => aname, :level => level, })
519
581
  end_paragraph
520
582
  @out << make_headline(level, title, aname)
521
583
 
@@ -576,10 +638,34 @@ module TracWiki
576
638
  else # case str
577
639
  raise "Parse error at #{str[0,30].inspect}"
578
640
  end
641
+ @line_no += ($`+$&).count("\n")
579
642
  str = $'
580
643
  end
581
644
  end_paragraph
645
+ @headings.last[:eline] = @line_no - 1
582
646
  @out
583
647
  end
648
+
649
+ def aname_nice(aname, title)
650
+
651
+ if aname.nil? && id_from_heading?
652
+ aname = title.gsub /\s+/, '_'
653
+ if id_translit?
654
+ aname = Iconv.iconv('ascii//translit', 'utf-8', aname).join
655
+ end
656
+ end
657
+ return nil if aname.nil?
658
+ aname_ori = aname
659
+ count = 2
660
+ while @anames[aname]
661
+ aname = aname_ori + ".#{count}"
662
+ count+=1
663
+ end
664
+ @anames[aname] = true
665
+ aname
666
+ end
667
+
668
+
669
+
584
670
  end
585
671
  end
@@ -1,3 +1,3 @@
1
1
  module TracWiki
2
- VERSION = '0.1.9'
2
+ VERSION = '0.1.12'
3
3
  end
data/test/parser_test.rb CHANGED
@@ -1,9 +1,17 @@
1
1
  require 'trac-wiki'
2
+ require 'pp'
3
+
2
4
 
3
5
  class Bacon::Context
4
6
  def tc(html, wiki, options = {})
5
7
  TracWiki.render(wiki, options).should.equal html
6
8
  end
9
+ def h(hash, wiki, opts = {})
10
+ parser = TracWiki.parser(wiki, opts)
11
+ parser.to_html
12
+ #pp parser.headers
13
+ parser.headings.should == hash
14
+ end
7
15
  end
8
16
 
9
17
  describe TracWiki::Parser do
@@ -141,12 +149,14 @@ describe TracWiki::Parser do
141
149
  tc "<h1>Heading 1</h1>", "= Heading 1 ="
142
150
  tc "<h2>Heading 2</h2>", "== Heading 2 =="
143
151
  tc "<h3>Heading 3</h3>", "=== Heading 3 ==="
144
- tc "<a name=\"HE3\"/><h3>Heading 3</h3>", "=== Heading 3 === #HE3"
145
- tc "<a name=\"Heading-3\"/><h3>Heading 3</h3>", "=== Heading 3 === #Heading-3"
146
- tc "<a name=\"Heading/3\"/><h3>Heading 3</h3>", "=== Heading 3 === #Heading/3"
147
- tc "<a name=\"Heading/3\"/><h3>Heading 3</h3>", "=== Heading 3 === #Heading/3 "
148
- tc "<a name=\"Heading&lt;3&gt;\"/><h3>Heading 3</h3>", "=== Heading 3 === #Heading<3>"
149
- tc "<a name=\"Heading'&quot;3&quot;'\"/><h3>Heading 3</h3>", "=== Heading 3 === #Heading'\"3\"'"
152
+ tc "<h3 id=\"HE3\">Heading 3</h3>", "=== Heading 3 === #HE3"
153
+ tc "<h3 id=\"Heading-3\">Heading 3</h3>", "=== Heading 3 === #Heading-3"
154
+ tc "<h3 id=\"Heading/3\">Heading 3</h3>", "=== Heading 3 === #Heading/3"
155
+ tc "<h3 id=\"Heading/3\">Heading 3</h3>", "=== Heading 3 === #Heading/3 "
156
+ tc "<h3 id=\"Heading/3\">Heading 3</h3><h3 id=\"Heading/3.2\">Heading 3</h3>",
157
+ "=== Heading 3 === #Heading/3\n=== Heading 3 === #Heading/3\n "
158
+ tc "<h3 id=\"Heading&lt;3&gt;\">Heading 3</h3>", "=== Heading 3 === #Heading<3>"
159
+ tc "<h3 id=\"Heading'&quot;3&quot;'\">Heading 3</h3>", "=== Heading 3 === #Heading'\"3\"'"
150
160
  # WARNING: Optional feature, not specified in
151
161
  tc "<h4>Heading 4</h4>", "==== Heading 4 ===="
152
162
  tc "<h5>Heading 5</h5>", "===== Heading 5 ====="
@@ -182,6 +192,8 @@ describe TracWiki::Parser do
182
192
  it 'should parse links' do
183
193
  # Links
184
194
  tc "<p><a href=\"link\">link</a></p>\n", "[[link]]"
195
+ tc "<p><a href=\"link#link\">link#link</a></p>\n", "[[link#link]]"
196
+ tc "<p><a href=\"#link\">#link</a></p>\n", "[[#link]]"
185
197
 
186
198
  # Links can appear in paragraphs (i.e. inline item)
187
199
  tc "<p>Hello, <a href=\"world\">world</a></p>\n", "Hello, [[world]]"
@@ -191,11 +203,12 @@ describe TracWiki::Parser do
191
203
 
192
204
  # URLs
193
205
  tc "<p><a href=\"http://www.example.org/\">http://www.example.org/</a></p>\n", "[[http://www.example.org/]]"
206
+ tc "<p><a href=\"http://www.example.org/#anch\">http://www.example.org/#anch</a></p>\n", "[[http://www.example.org/#anch]]"
194
207
 
195
208
  # Single punctuation characters at the end of URLs
196
209
  # should not be considered a part of the URL.
197
210
  [',','.','?','!',':',';','\'','"'].each do |punct|
198
- esc_punct = CGI::escapeHTML(punct)
211
+ esc_punct = TracWiki::Parser.escapeHTML(punct)
199
212
  tc "<p><a href=\"http://www.example.org/\">http://www.example.org/</a>#{esc_punct}</p>\n", "http://www.example.org/#{punct}"
200
213
  end
201
214
  # Nameds URLs (by example)
@@ -803,5 +816,43 @@ describe TracWiki::Parser do
803
816
 
804
817
  tc "<h6></h6><p>ahoj</p>\n", "=======\nahoj\n", :merge => false
805
818
  end
819
+ it 'should compute headers' do
820
+ h( [ {:level=>0, :sline=>1, :eline=>2},
821
+ {:title=>"ahoj", :sline=>3, :eline=> 5, :aname=>nil, :level=>2},
822
+ ],
823
+ "\nahoj\n== ahoj ==\nbhoj\nchoj\n")
824
+ h( [ {:level=>0, :sline=>1, :eline=>2},
825
+ {:title=>"ahoj", :sline=>3, :eline => 5, :aname=>nil, :level=>2},
826
+ {:title=>"dhoj", :sline=>6, :eline => 7, :aname=>nil, :level=>3},
827
+ ],
828
+ "\nahoj\n== ahoj ==\nbhoj\nchoj\n===dhoj===\nkuk\n")
829
+ h( [ {:level=>0, :sline=>1, :eline=>2},
830
+ {:title=>"ahoj", :sline=>3, :eline => 7, :aname=>nil, :level=>2},
831
+ {:title=>"dhoj", :sline=>8, :eline => 9, :aname=>nil, :level=>3},
832
+ ],
833
+ "\nahoj\n== ahoj ==\nbhoj\nchoj\n\n\n===dhoj===\nkuk\n")
834
+ h( [ {:level=>0, :sline=>1, :eline=>2},
835
+ {:title=>"ah o ~'j", :sline=>3, :eline => 5, :aname=>nil, :level=>2},
836
+ {:title=>"*dhoj", :sline=>6, :eline => 7, :aname=>'ble', :level=>3},
837
+ ],
838
+ "\nahoj\n== ah o ~'j ==\nbhoj\nchoj\n===*dhoj ===#ble\nkuk\n")
839
+ h( [ {:level=>0, :sline=>1, :eline=>2},
840
+ {:title=>"ah o ~'j", :sline=>3, :eline => 8, :aname=>nil, :level=>2},
841
+ {:title=>"*dhoj", :sline=>9, :eline => 11, :aname=>'ble', :level=>3},
842
+ ], <<eos)
843
+
844
+ ahoj
845
+ == ah o ~'j ==
846
+ {{{
847
+ ==a1.5hoj==
848
+ }}}
849
+
850
+
851
+ ===*dhoj ===#ble
852
+ kuk
853
+
854
+ eos
855
+
856
+ end
806
857
  end
807
858
  # vim: tw=0
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trac-wiki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
5
- prerelease:
4
+ version: 0.1.12
6
5
  platform: ruby
7
6
  authors:
8
7
  - Vitas Stradal
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-30 00:00:00.000000000 Z
11
+ date: 2013-10-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bacon
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: TracWiki markup language render (http://trac.edgewall.org/wiki/WikiFormatting
@@ -65,26 +60,25 @@ files:
65
60
  homepage: http://github.com/vitstradal/trac-wiki
66
61
  licenses:
67
62
  - GPL-2
63
+ metadata: {}
68
64
  post_install_message:
69
65
  rdoc_options: []
70
66
  require_paths:
71
67
  - lib
72
68
  required_ruby_version: !ruby/object:Gem::Requirement
73
- none: false
74
69
  requirements:
75
- - - ! '>='
70
+ - - '>='
76
71
  - !ruby/object:Gem::Version
77
72
  version: '0'
78
73
  required_rubygems_version: !ruby/object:Gem::Requirement
79
- none: false
80
74
  requirements:
81
- - - ! '>='
75
+ - - '>='
82
76
  - !ruby/object:Gem::Version
83
77
  version: '0'
84
78
  requirements: []
85
79
  rubyforge_project: trac-wiki
86
- rubygems_version: 1.8.23
80
+ rubygems_version: 2.0.3
87
81
  signing_key:
88
- specification_version: 3
82
+ specification_version: 4
89
83
  summary: Trac Wiki markup language
90
84
  test_files: []