redcloth-formatters-ast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redcloth-formatters-rdoc.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thomas Fankhauser
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,33 @@
1
+ h1. Redcloth::Formatters::AST
2
+
3
+ A RedCloth formatter that enables to convert @.textile@ files to an AST.
4
+
5
+ h2. Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ bc.. gem 'redcloth-formatters-ast'
10
+
11
+ p. And then execute:
12
+
13
+ bc.. $ bundle
14
+
15
+ p. Or install it yourself as:
16
+
17
+ bc.. $ gem install redcloth-formatters-ast
18
+
19
+ h2. Usage
20
+
21
+ bc.. # Compile a textile to an AST hash
22
+ textile_file = File.read 'a.textile'
23
+ ast_hash = RedCloth.new(textile_file).to_ast
24
+
25
+ # Do something with the AST
26
+
27
+ h2. Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,215 @@
1
+ module RedCloth::Formatters::AST
2
+ include RedCloth::Formatters::Base
3
+
4
+ @@ast = []
5
+
6
+ [:title, :subtitle, :author, :copyright, :h0, :h1, :h2, :h3, :h4, :cite].each do |m|
7
+ define_method(m) do |opts|
8
+ @@ast << { :type => m, :text => opts[:text], :options => parse_css(opts[:style]) }
9
+ ""
10
+ end
11
+ end
12
+
13
+ def strong(opts)
14
+ "<b>#{opts[:text]}</b>"
15
+ end
16
+
17
+ def em(opts)
18
+ "<i>#{opts[:text]}</i>"
19
+ end
20
+
21
+ def p(opts)
22
+ return "" if opts[:text] =~ /^[\n]$+/
23
+ if opts[:append_to_parent]
24
+ @@ast.last[:text] << "#{opts[:text]}\n\n"
25
+ else
26
+ @@ast << { :type => :p, :text => opts[:text], :options => parse_css(opts[:style]) }
27
+ end
28
+ ""
29
+ end
30
+
31
+ # lists
32
+ @@list_levels = { :ul => 0, :ol => 0, :last => nil }
33
+ def ul_open(opts)
34
+ @@ast << { :type => :unnumbered_list, :items => [], :options => parse_css(opts[:style]) } if @@list_levels[:ul] == 0
35
+ @@list_levels[:ul] += 1
36
+ @@list_levels[:last] = :ul
37
+ ""
38
+ end
39
+
40
+ def ul_close(opts)
41
+ @@list_levels[:ul] -= 1
42
+ ""
43
+ end
44
+
45
+ @@ol_level = 0
46
+ def ol_open(opts)
47
+ @@ast << { :type => :numbered_list, :items => [], :options => parse_css(opts[:style]) } if @@list_levels[:ol] == 0
48
+ @@list_levels[:ol] += 1
49
+ @@list_levels[:last] = :ol
50
+ ""
51
+ end
52
+
53
+ def ol_close(opts)
54
+ @@list_levels[:ol] -= 1
55
+ ""
56
+ end
57
+
58
+ def li_open(opts)
59
+ @@ast.last[:items] << { :type => :list_item, :text => opts[:text], :level => @@list_levels[@@list_levels[:last]]-1, :options => parse_css(opts[:style]) }
60
+ ""
61
+ end
62
+
63
+ def li_close(opts=nil)
64
+ ""
65
+ end
66
+
67
+ # tables
68
+ def table_open(opts)
69
+ options = parse_css(opts[:style])
70
+ @@ast << { :type => :table, :title => options[:name], :header => false, :rows => [], :options => options }
71
+ ""
72
+ end
73
+
74
+ def table_close(opts)
75
+ ""
76
+ end
77
+
78
+ def tr_open(opts)
79
+ @@ast.last[:rows] << []
80
+ ""
81
+ end
82
+
83
+ def td(opts)
84
+ @@ast.last[:header] = true if opts[:th]
85
+ @@ast.last[:rows].last << { :type => :table_cell, :text => opts[:text].strip, :header => !opts[:th].nil? }
86
+ ""
87
+ end
88
+
89
+
90
+ # footnotes
91
+ def footno(opts)
92
+ opts[:id] ||= opts[:text]
93
+ "<sup>#{opts[:id]}</sup>"
94
+ end
95
+
96
+ def fn(opts)
97
+ @@ast << { :type => :footnote, :id => opts[:id], :text => opts[:text], :options => parse_css(opts[:style]) }
98
+ ""
99
+ end
100
+
101
+ def link(opts)
102
+ @@ast << { :type => :link, :href => opts[:href], :name => opts[:name], :options => parse_css(opts[:style]) }
103
+ ""
104
+ end
105
+
106
+ def image(opts)
107
+ title = opts[:title]
108
+ id = title.split(" ").join(".")
109
+ options = parse_css(opts[:style])
110
+ width = options[:width] || "100%"
111
+
112
+ @@ast << { :type => :image, :title => title, :src => opts[:src], :width => width, :options => options }
113
+ ""
114
+ end
115
+
116
+ def bq_open(opts)
117
+ opts[:block] = true
118
+ opts[:append_to_parent] = true
119
+ options = parse_css(opts[:style])
120
+ @@ast << { :type => :blockquote, :text => "", :by => options[:by], :options => options }
121
+ ""
122
+ end
123
+
124
+ def bq_close(opts)
125
+ opts[:append_to_parent] = false
126
+ ""
127
+ end
128
+
129
+ # source
130
+ def bc_open(opts)
131
+ opts[:block] = true
132
+ opts[:append_to_parent] = true
133
+ options = parse_css(opts[:style])
134
+ @@ast << { :type => :code, :text => "", :title => options[:name], :options => options }
135
+ ""
136
+ end
137
+
138
+ def code(opts)
139
+ if opts[:append_to_parent]
140
+ @@ast.last[:text] << "#{opts[:text]}\n\n"
141
+ else
142
+ @@ast << { :type => :code, :text => opts[:text], :options => parse_css(opts[:style]) }
143
+ end
144
+ ""
145
+ end
146
+
147
+ def bc_close(opts)
148
+ opts[:append_to_parent] = false
149
+ ""
150
+ end
151
+
152
+ def self.ast
153
+ @@ast
154
+ end
155
+
156
+ def self.reset!
157
+ @@ast = []
158
+ end
159
+
160
+ private
161
+
162
+ def parse_css css
163
+ options = {}
164
+ begin
165
+ css = css.split ";"
166
+ css.each do |css|
167
+ key, value = css.split ":"
168
+ options[key.strip.to_sym] = value.strip
169
+ end
170
+ rescue
171
+ end
172
+ options
173
+ end
174
+
175
+ def parse_id name
176
+ name.gsub(",", "").gsub(".", "").squeeze(" ").strip.gsub(" ", "")
177
+ end
178
+
179
+ def clean_text(text)
180
+ text.gsub("\n", " ").gsub("\t", " ").squeeze(" ").strip
181
+ end
182
+
183
+ # escapement for regular HTML (not in PRE tag)
184
+ def escape(text)
185
+ text
186
+ end
187
+
188
+ # escapement for HTML in a PRE tag
189
+ def escape_pre(text)
190
+ text
191
+ end
192
+
193
+ # escaping for HTML attributes
194
+ def escape_attribute(text)
195
+ text
196
+ end
197
+
198
+ def after_transform(text)
199
+ text.chomp!
200
+ end
201
+
202
+
203
+ def before_transform(text)
204
+
205
+ end
206
+
207
+ def method_missing(method, opts)
208
+ opts[:text] || ""
209
+ end
210
+
211
+ def formatter_methods
212
+ singleton_methods.map! {|method| method.to_sym }
213
+ end
214
+
215
+ end
@@ -0,0 +1,18 @@
1
+ module RedCloth
2
+ class TextileDoc
3
+
4
+ #
5
+ # Generates an AST from the Textile contents.
6
+ #
7
+ # RedCloth.new( "!some/image/path.png!" ).to_ast
8
+ # #=> { :type => :image, :src => "some/image/path.png" }
9
+ #
10
+ def to_ast(*rules)
11
+ apply_rules(rules)
12
+
13
+ RedCloth::Formatters::AST.reset!
14
+ to(RedCloth::Formatters::AST)
15
+ RedCloth::Formatters::AST.ast
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module Redcloth
2
+ module Formatters
3
+ module AST
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'RedCloth'
2
+
3
+ require 'redcloth-formatters-ast/version'
4
+ require 'redcloth-formatters-ast/formatters/ast'
5
+ require 'redcloth-formatters-ast/textile_doc'
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/redcloth-formatters-ast/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Thomas Fankhauser"]
6
+ gem.email = ["tommylefunk@googlemail.com"]
7
+ gem.description = %q{A RedCloth formatter that compiles textile to an AST}
8
+ gem.summary = %q{A RedCloth formatter that enables to convert *.textile files to a AST hash. This is usefull for further processing.}
9
+ gem.homepage = "http://southdesign.github.com/redcloth-formatters-ast"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "redcloth-formatters-ast"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Redcloth::Formatters::AST::VERSION
17
+ end
data/spec/ast_spec.rb ADDED
@@ -0,0 +1,98 @@
1
+ require_relative "./helpers"
2
+
3
+ describe RedCloth::Formatters::AST do
4
+
5
+ it "should compile title, subtitle, author, copyright, h0, h1, h2, h3, h4, i, em, b, strong, cite" do
6
+ textile = File.read "spec/fixtures/title_subtitle.textile"
7
+ RedCloth.new(textile).to_ast.should == [
8
+ { :type => :title, :text => "Sample Document", :options => {}},
9
+ { :type => :subtitle, :text => "How to load multiple documents", :options => {}},
10
+ { :type => :author, :text => "Thomas Fankhauser", :options => {}},
11
+ { :type => :copyright, :text => "2010, Thomas Fankhauser, All rights reserved.", :options => {}},
12
+ { :type => :h0, :text => "Application", :options => {}},
13
+ { :type => :h1, :text => "Board", :options => {}},
14
+ { :type => :h2, :text => "Names", :options => {}},
15
+ { :type => :h3, :text => "Meetings", :options => {}},
16
+ { :type => :h4, :text => "Tests", :options => {}},
17
+ { :type => :p, :text => "Some <b>bold</b> text. And some <i>italic</i> text.", :options => {}}
18
+ ]
19
+ end
20
+
21
+ it "should compile lists" do
22
+ textile = File.read "spec/fixtures/lists.textile"
23
+ RedCloth.new(textile).to_ast.should == [
24
+ { :type => :unnumbered_list, :items => [
25
+ { :type => :list_item, :text => "First Entry", :level => 0, :options => {}},
26
+ { :type => :list_item, :text => "Second Entry", :level => 0, :options => {}},
27
+ { :type => :list_item, :text => "Sub Entry 1", :level => 1, :options => {}},
28
+ { :type => :list_item, :text => "Sub Entry 2", :level => 1, :options => {}},
29
+ { :type => :list_item, :text => "Third Entry", :level => 0, :options => {}}
30
+ ], :options => {}},
31
+
32
+ { :type => :numbered_list, :items => [
33
+ { :type => :list_item, :text => "A numbered entry", :level => 0, :options => {}},
34
+ { :type => :list_item, :text => "A sub numbered entry", :level => 1, :options => {}},
35
+ { :type => :list_item, :text => "Another numbered entry", :level => 0, :options => {}}
36
+ ], :options => {}}
37
+ ]
38
+ end
39
+
40
+ it "should compile tables" do
41
+ textile = File.read "spec/fixtures/tables.textile"
42
+ RedCloth.new(textile).to_ast.should == [
43
+ { :type => :table, :title => "Attendees", :header => true, :rows => [
44
+ [
45
+ { :type => :table_cell, :text => "Name", :header => true },
46
+ { :type => :table_cell, :text => "Age", :header => true },
47
+ { :type => :table_cell, :text => "Sex", :header => true }
48
+ ],
49
+ [
50
+ { :type => :table_cell, :text => "Joan", :header => false },
51
+ { :type => :table_cell, :text => "24", :header => false },
52
+ { :type => :table_cell, :text => "F", :header => false }
53
+ ],
54
+ [
55
+ { :type => :table_cell, :text => "Archie", :header => false },
56
+ { :type => :table_cell, :text => "29", :header => false },
57
+ { :type => :table_cell, :text => "M", :header => false }
58
+ ],
59
+ [
60
+ { :type => :table_cell, :text => "Bello", :header => false },
61
+ { :type => :table_cell, :text => "45", :header => false },
62
+ { :type => :table_cell, :text => "F", :header => false }
63
+ ]
64
+ ], :options => {:name => "Attendees", :cols => "3", :align => "left"}
65
+ }
66
+ ]
67
+ end
68
+
69
+ it "should compile footnotes" do
70
+ textile = File.read "spec/fixtures/footnotes.textile"
71
+ RedCloth.new(textile).to_ast.should == [
72
+ {:type=>:p, :text=>"This is a normal text<sup>1</sup> with a footnote in it.", :options=>{}},
73
+ {:type=>:p, :text=>"And here is some more text with even more footnotes<sup>2</sup> in it.", :options=>{}},
74
+ {:type=>:footnote, :id=>"1", :text=>"This is the actual footnote.", :options=>{}},
75
+ {:type=>:footnote, :id=>"2", :text=>"A Footnote is this.", :options=>{}}]
76
+ end
77
+
78
+ it "should compile links and images" do
79
+ textile = File.read "spec/fixtures/link_image.textile"
80
+ RedCloth.new(textile).to_ast.should == [
81
+ { :type=>:link, :href=>"http://example.com", :name=>"Text to display", :options=>{}},
82
+ { :type=>:link, :href=>"http://example.com", :name=>"Text to display", :options=>{}},
83
+ { :type=>:image, :title=>"A great image", :src=>"../images/image.png", :width=>"100%", :options=>{}},
84
+ { :type=>:image, :title=>"A great image", :src=>"../images/image.png", :width=>"90%", :options=>{:width=>"90%"}},
85
+ ]
86
+ end
87
+
88
+ it "should compile blockquotes and source" do
89
+ textile = File.read "spec/fixtures/blockquote_source.textile"
90
+ RedCloth.new(textile).to_ast.should == [
91
+ { :type => :blockquote, :text => "This is a blockquote\nthat spans multiple lines.\n\nAnd continues here.\n\n", :by=>"Thomas Fankhauser", :options=>{:by=>"Thomas Fankhauser"}},
92
+ { :type => :code, :text => "module RedCloth\n class TextileDoc\n\n #\n # Generates an AST from the Textile contents.\n #\n # RedCloth.new( \"!some/image/path.png!\" ).to_ast\n # #=> { :type => :image, :src => \"some/image/path.png\" }\n #\n def to_ast(*rules)\n apply_rules(rules)\n\n RedCloth::Formatters::AST.reset!\n to(RedCloth::Formatters::AST)\n RedCloth::Formatters::AST.ast\n end\n end\nend\n\n", :title=>"A Ruby Racer", :options=>{:name=>"A Ruby Racer"}}
93
+ ]
94
+ end
95
+
96
+
97
+
98
+ end
@@ -0,0 +1,23 @@
1
+ bq{by:Thomas Fankhauser;}.. This is a blockquote
2
+ that spans multiple lines.
3
+
4
+ And continues here.
5
+
6
+ bc{name:A Ruby Racer}.. module RedCloth
7
+ class TextileDoc
8
+
9
+ #
10
+ # Generates an AST from the Textile contents.
11
+ #
12
+ # RedCloth.new( "!some/image/path.png!" ).to_ast
13
+ # #=> { :type => :image, :src => "some/image/path.png" }
14
+ #
15
+ def to_ast(*rules)
16
+ apply_rules(rules)
17
+
18
+ RedCloth::Formatters::AST.reset!
19
+ to(RedCloth::Formatters::AST)
20
+ RedCloth::Formatters::AST.ast
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ This is a normal text[1] with a footnote in it.
2
+
3
+ And here is some more text with even more footnotes[2] in it.
4
+
5
+ fn1. This is the actual footnote.
6
+
7
+ fn2. A Footnote is this.
@@ -0,0 +1,4 @@
1
+ "Text to display":http://example.com
2
+ "Text to display (Title text)":http://example.com
3
+ !../images/image.png(A great image)!
4
+ !{width: 90%;}../images/image.png(A great image)!
@@ -0,0 +1,9 @@
1
+ * First Entry
2
+ * Second Entry
3
+ ** Sub Entry 1
4
+ ** Sub Entry 2
5
+ * Third Entry
6
+
7
+ # A numbered entry
8
+ ## A sub numbered entry
9
+ # Another numbered entry
@@ -0,0 +1,5 @@
1
+ table{name:Attendees; cols:3; align: left;}.
2
+ |_. Name |_. Age |_. Sex |
3
+ | Joan | 24 | F |
4
+ | Archie | 29 | M |
5
+ | Bello | 45 | F |
@@ -0,0 +1,20 @@
1
+ title. Sample Document
2
+
3
+ subtitle. How to load multiple documents
4
+
5
+ author. Thomas Fankhauser
6
+
7
+ copyright. 2010, Thomas Fankhauser, All rights reserved.
8
+
9
+
10
+ h0. Application
11
+
12
+ h1. Board
13
+
14
+ h2. Names
15
+
16
+ h3. Meetings
17
+
18
+ h4. Tests
19
+
20
+ Some *bold* text. And some _italic_ text.
data/spec/helpers.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'redcloth-formatters-ast'
2
+
3
+ module Helpers
4
+ end
5
+
6
+ RSpec.configure do |config|
7
+ config.include Helpers
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redcloth-formatters-ast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Fankhauser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A RedCloth formatter that compiles textile to an AST
15
+ email:
16
+ - tommylefunk@googlemail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.textile
25
+ - Rakefile
26
+ - lib/redcloth-formatters-ast.rb
27
+ - lib/redcloth-formatters-ast/formatters/ast.rb
28
+ - lib/redcloth-formatters-ast/textile_doc.rb
29
+ - lib/redcloth-formatters-ast/version.rb
30
+ - redcloth-formatters-ast.gemspec
31
+ - spec/ast_spec.rb
32
+ - spec/fixtures/blockquote_source.textile
33
+ - spec/fixtures/footnotes.textile
34
+ - spec/fixtures/link_image.textile
35
+ - spec/fixtures/lists.textile
36
+ - spec/fixtures/tables.textile
37
+ - spec/fixtures/title_subtitle.textile
38
+ - spec/helpers.rb
39
+ homepage: http://southdesign.github.com/redcloth-formatters-ast
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.11
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: A RedCloth formatter that enables to convert *.textile files to a AST hash.
63
+ This is usefull for further processing.
64
+ test_files:
65
+ - spec/ast_spec.rb
66
+ - spec/fixtures/blockquote_source.textile
67
+ - spec/fixtures/footnotes.textile
68
+ - spec/fixtures/link_image.textile
69
+ - spec/fixtures/lists.textile
70
+ - spec/fixtures/tables.textile
71
+ - spec/fixtures/title_subtitle.textile
72
+ - spec/helpers.rb