raf 0.0.0 → 0.0.1

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/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/History.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == 0.0.1 2011-04-07
2
+ * first public version
data/LICENSE.txt CHANGED
@@ -1,20 +1,14 @@
1
- Copyright (c) 2011 garin
1
+ Copyright (C) garin <garin54@gmail.com> 2011
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
10
7
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
13
12
 
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ You should have received a copy of the GNU General Public License
14
+ along with Foobar. If not, see <http://www.gnu.org/licenses/>.
data/README.rdoc CHANGED
@@ -1,19 +1,8 @@
1
1
  = raf
2
-
3
- Description goes here.
4
-
5
- == Contributing to raf
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
2
+ "raf" is simple document format.
3
+ refer to : http://garin.jp/doc/Software/raf (this page is japanese)
14
4
 
15
5
  == Copyright
16
-
17
6
  Copyright (c) 2011 garin. See LICENSE.txt for
18
7
  further details.
19
8
 
data/Rakefile CHANGED
@@ -14,9 +14,9 @@ Jeweler::Tasks.new do |gem|
14
14
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
15
  gem.name = "raf"
16
16
  gem.homepage = "http://github.com/garin/raf"
17
- gem.license = "MIT"
18
- gem.summary = %Q{one-line summary of your gem}
19
- gem.description = %Q{longer description of your gem}
17
+ gem.license = "GPL"
18
+ gem.summary = %Q{raf is simple document format}
19
+ gem.description = %Q{raf is simple document format}
20
20
  gem.email = "garin54@gmail.com"
21
21
  gem.authors = ["garin"]
22
22
  # Include your dependencies below. Runtime dependencies are required when using your gem,
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/bin/raf2html ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift "/home/yoshino/src/raf/lib"
3
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
4
+ require "optparse"
5
+ require "rafblockparser.tab"
6
+ require "raf2html"
7
+
8
+ options = {:css => nil, :language => 'ja', :index => true, :quiet => false}
9
+ opt = OptionParser.new do |opt|
10
+ opt.on("--css file", "スタイルシートのファイルを指定") {|f| options[:css] = f}
11
+ opt.on("-L", "--language LANG", "言語(デフォルトは #{options[:language]} 。ドキュメント側で指定されていればそちらを優先)"){|l| options[:language] = l}
12
+ opt.on("--[no-]index", "目次を生成する(デフォルトは生成する)"){|v| options[:index] = v }
13
+ opt.on("-q","--quiet", "本文だけを出力する(ヘッダやフッタは出力しない)"){ options[:quiet] = true }
14
+ end
15
+ opt.parse!(ARGV)
16
+ src = File.open(ARGV[0],"r").readlines
17
+ raf = Raf::Raf2Html.new(src, options)
18
+ puts raf.to_html
data/lib/default.css ADDED
@@ -0,0 +1,116 @@
1
+ /* ----- default css ----- */
2
+
3
+ body {
4
+ font-size : 95%;
5
+ }
6
+
7
+ /* ----- header ----- */
8
+ h1,h2,h3,h4,h5,h6 {
9
+ color : #000000;
10
+ }
11
+ h1 a,h2 a,h3 a,h4 a,h5 a,h6 a {
12
+ color : #000000;
13
+ text-decoration : none;
14
+ border-bottom : 0px ;
15
+ }
16
+
17
+
18
+ h1 {
19
+ font-size : 300%;
20
+ padding : 1em;
21
+ margin : 0em;
22
+ border : 2px solid black;
23
+ text-align : center;
24
+ }
25
+ h1 a {
26
+ font-size :0%;
27
+ }
28
+
29
+ h2 {
30
+ font-size: 170%;
31
+ border-bottom : 3px solid black;
32
+ margin-top : 2em;
33
+ padding-bottom : 0.3em;
34
+ padding-left: 0.1em;
35
+ }
36
+
37
+ h3 {
38
+ font-size: 150%;
39
+ margin-top : 2em;
40
+ }
41
+ h4 {
42
+ font-size: 130%;
43
+ }
44
+ h5 {
45
+ font-size: 100%;
46
+ }
47
+ h6 {
48
+ font-size: 100%;
49
+ }
50
+
51
+ /* ----- list ----- */
52
+ ul {
53
+ margin-left : 0em;
54
+ padding-left : 1em;
55
+ }
56
+ li {
57
+ padding: 0px;
58
+ }
59
+ li p {
60
+ margin : 0px;
61
+ }
62
+
63
+ li a {
64
+ text-decoration : none;
65
+ }
66
+
67
+
68
+ /* ----- dl ----- */
69
+ dl{
70
+ margin-left : 0em;
71
+ }
72
+ dt {
73
+ font-weight: bold;
74
+ }
75
+
76
+ dt a {
77
+ border-bottom : 1px dotted black;
78
+ margin-bottom: 2px;
79
+ }
80
+ dd{
81
+ margin-left: 1em;
82
+ margin-bottom : 1em;
83
+ color : #303030;
84
+ }
85
+ dd p {
86
+ margin : 0px;
87
+ }
88
+
89
+ /* ----- table ----- */
90
+ table {
91
+ border: 3px solid black;
92
+ border-collapse:collapse;
93
+ }
94
+ tr {
95
+
96
+ }
97
+ th{
98
+ border: 2px solid black;
99
+ font-weight: bold;
100
+ background-color : #e0e0e0;
101
+ padding: 0.2em 1em;
102
+ }
103
+ td {
104
+ padding: 0.2em 1em;
105
+ border: 1px solid black;
106
+ }
107
+
108
+ /* ----- pre ----- */
109
+ pre {
110
+ font-family : monospace !important;
111
+ margin: 1em 0em 1em 0em;
112
+ padding : 0.5em 0em 0.5em 0em;
113
+ background-color: #000000;
114
+ color : #f0f0f0;
115
+ border : 2px solid #303030;
116
+ }
@@ -0,0 +1,18 @@
1
+ module ParserUtility
2
+ def add_children_to_element(obj, *val)
3
+ obj.add = *val
4
+ end
5
+
6
+ def mark_to_level(mark_str)
7
+ mark2level_list = {
8
+ "=" => 1,
9
+ "==" => 2,
10
+ "===" => 3,
11
+ "====" => 4,
12
+ "+" => 5,
13
+ "++" => 6
14
+ }
15
+ mark2level_list[mark_str] or
16
+ raise ArgumentError, "#{mark_str} is irregular for Headline mark."
17
+ end
18
+ end
data/lib/raf2html.rb ADDED
@@ -0,0 +1,167 @@
1
+ require "raf2html_element"
2
+ require "cgi"
3
+
4
+ module Raf
5
+ class Raf2Html
6
+ def initialize(src, options = {})
7
+ @debug = true
8
+
9
+ # options
10
+ @css = File.open(options[:css]).readlines.to_s unless options[:css].nil?
11
+ @language = options[:language]
12
+ @index = options[:index]
13
+ @quiet = options[:quiet]
14
+
15
+ @raf = BlockParser.new
16
+ @info = setup_info
17
+ @nodes = @raf.parse src.map {|s| CGI.escapeHTML(s) }
18
+ end
19
+
20
+ def setup_info
21
+ info = @raf.info
22
+ info[:language] = @language if info[:language].nil?
23
+ info
24
+ end
25
+
26
+ def to_html
27
+ html = ""
28
+ html += header unless @quiet
29
+ html += header_title
30
+ html += metadata
31
+ html += index if @index
32
+ html += body
33
+ html += footnote
34
+ html += footer unless @quiet
35
+ html
36
+ end
37
+
38
+ def body
39
+ @nodes.map do |node| node.apply end.to_s
40
+ end
41
+
42
+
43
+ def index
44
+ return "" if @raf.index[:head].nil?
45
+ str = "<div id='raf-index'>"
46
+ level_pre = 1
47
+ @raf.index[:head].each_with_index do |h,i|
48
+ next if h[:level] == 1 or h[:level] == 6
49
+
50
+ if h[:level] == 5
51
+ str += '<br />'
52
+ str += "<a href='#raf-head#{h[:level]}-#{i+1}' style='padding-left: 1em'>#{h[:title]}</a>\n"
53
+ else
54
+ str += index_terminate(h[:level], level_pre)
55
+ str += "<li><a href='#raf-head#{h[:level]}-#{i+1}'>#{h[:index]} #{h[:title]}</a>\n"
56
+ level_pre = h[:level]
57
+ end
58
+ end
59
+ str += index_terminate(2, level_pre) + "</ul>"
60
+ str += "</div>"
61
+ str
62
+ end
63
+
64
+ def index_terminate(level, level_pre)
65
+ str = ""
66
+ case level <=> level_pre
67
+ when 1
68
+ (level - level_pre).times do
69
+ str += "<ul>"
70
+ end
71
+ when -1
72
+ (level_pre - level).times do
73
+ str += "</ul></li>"
74
+ end
75
+ else
76
+ str += "</li>"
77
+ end
78
+ str
79
+ end
80
+
81
+ def metadata
82
+ str = "<div id='raf-metadata'>"
83
+ str += "<table id='raf-metadata-table'>"
84
+ str_pre = ""
85
+ str_pre += "<tr><th>著者</th><td colspan='3'>#{@info[:creator]}</td></tr>" unless @info[:creator].nil?
86
+
87
+ unless @info[:date].nil? or @info[:update].nil?
88
+ str_pre += "<tr>"
89
+ str_pre += "<th>作成日</th><td>#{@info[:date]}</td>" unless @info[:date].nil?
90
+ str_pre += "<th>更新日</th><td>#{@info[:update]}</td>" unless @info[:update].nil?
91
+ str_pre += "</tr>"
92
+ end
93
+
94
+ unless @info[:publisher].nil? or @info[:version].nil?
95
+ str_pre += "<tr>"
96
+ str_pre += "<th>発行</th><td>#{@info[:publisher]}</td>" unless @info[:publisher].nil?
97
+ str_pre += "<th>バージョン</th><td>#{@info[:version]}</td>" unless @info[:version].nil?
98
+ str_pre += "</tr>"
99
+ end
100
+
101
+ unless @info[:contributor].nil? or @info[:revison].nil?
102
+ str_pre += "<tr>"
103
+ str_pre += "<th>寄稿者</th>#{@info[:contributor]}</td>" unless @info[:contributor].nil?
104
+ str_pre += "<th>リビジョン</th><td>#{@info[:revison]}</td>" unless @info[:revison].nil?
105
+ str_pre += "</tr>"
106
+ end
107
+
108
+ str_pre += "<tr><th>概要</th><td colspan='4'>#{@info[:description]}</td></tr>" unless @info[:description].nil?
109
+
110
+ if str_pre.empty?
111
+ str_pre
112
+ else
113
+ str + str_pre + "\n</table>\n</div>"
114
+ end
115
+ end
116
+
117
+ def footnote
118
+ return "" if @raf.inline_index[:footnote].nil?
119
+ str = "<div id='raf-footnote'>"
120
+ @raf.inline_index[:footnote].each_with_index do |f,i|
121
+ str += "<a id='raf-footnote-#{i+1}' />"
122
+ str += "<a href='#raf-footnote-#{i+1}-reverse'>*#{i+1}</a>"
123
+ str += " #{f[:content].map{|c| c.apply}}<br />"
124
+ end
125
+ str += "</div>"
126
+ str
127
+ end
128
+
129
+ def header
130
+ str = <<EOL
131
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
132
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
133
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="#{@info[:language]}">
134
+ <head>
135
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
136
+ EOL
137
+ str += css
138
+ str += <<EOL
139
+ <title>#{@info[:subject]}</title>
140
+ </head>
141
+ <body>
142
+ EOL
143
+ end
144
+
145
+ def header_title
146
+ "<h1>#{@info[:subject]}</h1>\n"
147
+ end
148
+
149
+ def css
150
+ str = <<EOL
151
+ <link href="screen.css" rel="stylesheet" type="text/css" media="screen" />
152
+ <link href="handheld.css" rel="stylesheet" type="text/css" media="handheld" />
153
+ <link href="print.css" rel="stylesheet" type="text/css" media="print" />
154
+ EOL
155
+ str += %%<style type="text/css"><!--\n#{@css}\n--></style>\n% unless @css.nil?
156
+ str
157
+ end
158
+
159
+ def footer
160
+ str = "\n"
161
+ str += "<div id='rights'>#{@info[:rights]}</div>\n" unless @info[:rights].nil?
162
+ str += "</body>\n</html>"
163
+ str
164
+ end
165
+ end
166
+ end
167
+
@@ -0,0 +1,195 @@
1
+ module Raf
2
+
3
+ class Element
4
+ def newline_to_br(str)
5
+ str.gsub("\n", "<br />\n")
6
+ end
7
+
8
+ # @inline_parser.parse の配列を文字列に変換する
9
+ def inline_parse_to_str(array)
10
+ str = ""
11
+ array.each do |obj|
12
+ str += obj.apply
13
+ end
14
+ str
15
+ end
16
+ end
17
+
18
+ # ----- Blocks
19
+ class WhiteLine < Element
20
+ def apply
21
+ "\n"
22
+ end
23
+ end
24
+
25
+ class PlainTextBlock < Element
26
+ def apply
27
+ @contents.map {|c| c.apply }
28
+ end
29
+ end
30
+
31
+ class HeadLine < Element
32
+ # @contents = [level, title, id, index]
33
+ def apply
34
+ return "" if @contents[0] == 1
35
+ str = ""
36
+ str += "<h#{@contents[0]} id='raf-head#{@contents[0]}-#{@contents[2]}'>"
37
+ str += "<a id='#{@contents[1].to_code}'></a>"
38
+ str += "#{@contents[3]}" unless @contents[0] == 1 or @contents[0] == 5 or @contents[0] == 6
39
+ str += "#{@contents[1]}</h#{@contents[0]}>\n"
40
+ str
41
+ end
42
+ end
43
+
44
+ class Paragraph < Element
45
+ def apply
46
+ "<p>#{@contents.map{|c| c.apply}}</p>\n"
47
+ end
48
+ end
49
+
50
+ class Quote < Element
51
+ def apply
52
+ "<pre>#{@contents}</pre>\n"
53
+ end
54
+ end
55
+
56
+ class ItemList < Element
57
+ def apply
58
+ return "" if @contents.empty?
59
+ type = :dummy
60
+ str = "<ul>\n"
61
+ @contents.each do |item|
62
+ type_pre = type
63
+ case item
64
+ when :INDENT
65
+ type = :indent
66
+ str += "\n<ul>\n"
67
+ when :DEDENT
68
+ type = :dedent
69
+ str += "</li>\n" if type_pre == :item
70
+ str += "</ul>\n</li>\n"
71
+ else
72
+ type = :item
73
+ str += "</li>\n" if type_pre == :item
74
+ str += "<li>#{item.apply}"
75
+ end
76
+ end
77
+ str += "\n</li>" if type == :item
78
+ str += "\n</ul>\n"
79
+ str
80
+ end
81
+ end
82
+
83
+ class NumList < Element
84
+ def apply
85
+ str = "<ol>\n"
86
+ @contents.map do |item|
87
+ if item == :INDENT
88
+ str += "<ol>\n"
89
+ elsif item == :DEDENT
90
+ str += "</ol>\n"
91
+ else
92
+ str += "<li>#{item.apply}</li>\n"
93
+ end
94
+ end
95
+ str += "</ol>\n"
96
+ str
97
+ end
98
+ end
99
+
100
+ class Desc < Element
101
+ # @contents = [title, lines]
102
+ def apply
103
+ str = "<dl id='#{@contents[0].to_code}'>\n<dt>#{@contents[0]}</dt>\n"
104
+ str += "<dd>#{inline_parse_to_str(@contents[1])}</dd>\n" unless inline_parse_to_str(@contents[1]).empty?
105
+ str += "</dl>"
106
+ str
107
+ end
108
+ end
109
+
110
+ class Table < Element
111
+ def apply
112
+ str = "<table>"
113
+ @contents.each do |line|
114
+ str += "\n<tr>"
115
+ line.each do |item|
116
+ if item =~ /^\s*\*/
117
+ str += "<th>#{item.sub(/^\s*\*/, "").sub(/\*\s*$/,"")}</th>"
118
+ else
119
+ str += "<td>#{item}</td>"
120
+ end
121
+ end
122
+ str += "\n</tr>"
123
+ end
124
+ str += "\n</table>"
125
+ str
126
+ end
127
+ end
128
+ # --- Blocks end
129
+
130
+ # --- Inlines
131
+ class Label < Element
132
+ # @contents = [label, title]
133
+ def apply
134
+ "<a href='##{@contents[0]}' id='#{@contents[0]}'>#{@contents[1]}</a>"
135
+ end
136
+ end
137
+
138
+ class Reference < Element
139
+ def apply
140
+ "<a href='#{@contents[1]}' title='#{@contents[1]}'>#{@contents[0]}</a>"
141
+ end
142
+ end
143
+
144
+
145
+ class Plain < Element
146
+ def apply
147
+ "#{newline_to_br(@contents.join(""))}"
148
+ end
149
+ end
150
+
151
+ class Emphasis < Element
152
+ def apply
153
+ "<em>#{@contents.map{|c| c.apply}}</em>"
154
+ end
155
+ end
156
+ class Italic < Element
157
+ def apply
158
+ "<i>#{@contents.map{|c| c.apply}}</i>"
159
+ end
160
+ end
161
+ class Strike < Element
162
+ def apply
163
+ "<s>#{@contents.map{|c| c.apply}}</s>"
164
+ end
165
+ end
166
+
167
+ class Verb < Element
168
+ def apply
169
+ "#{@contents}"
170
+ end
171
+ end
172
+
173
+ class Manuedo < Element
174
+ def apply
175
+ "<span class='manuedo'>/#{@contents.map{|c| c.apply}}/</span>"
176
+ end
177
+ end
178
+ class Ruby < Element
179
+ def apply
180
+ "<ruby><rb>#{@contents[0]}</rb><rp>(</rp><rt>#{@contents[1]}</rt><rp>)</rp></ruby>"
181
+ end
182
+ end
183
+
184
+ class Footnote < Element
185
+ def apply
186
+ "<a href='#raf-footnote-#{@contents[1]}' name='raf-footnote-#{@contents[1]}-reverse' title='#{@contents[0].map {|c| c.apply}}'><sup><small>*#{@contents[1]}</small></sup></a>"
187
+ end
188
+ end
189
+
190
+ class Image < Element
191
+ def apply
192
+ "<img src='#{@contents}' />"
193
+ end
194
+ end
195
+ end