-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg2textile.rb
executable file
·179 lines (162 loc) · 3.17 KB
/
org2textile.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env ruby
# getting to the point where I'll need to build a DOM
$confluence = ARGV.delete("--confluence")
$APP_DEBUG = ARGV.delete("--debug")
def slide
puts "\n!SLIDE\n"
end
def code_block(line)
if $confluence
"{code}\n#{line}\n{code}"
else
"<pre><code>\n#{line}\n</code></pre>"
end
end
def code_span(line)
if $confluence
"{{#{line}}}"
else
"@#{line.chomp}@"
end
end
def start_code_block
if $confluence
puts "{code}"
else
puts "<pre><code>"
end
end
def end_code_block
if $confluence
puts "{code}"
else
puts "</code></pre>"
end
end
def expand_link(link)
# http://en.wikipedia.com/wiki/
scheme, rest = link.split(/:/)
case link
when /wp:/
scheme = "http://en.wikipedia.com/wiki/"
when /[a-z+]:/
scheme = scheme + ":"
end
dbg [:scheme, scheme, :rest, rest].inspect
scheme + rest.to_s
end
def dbg(*a)
STDERR.puts "DEBUG: " + a.inspect if $APP_DEBUG
end
in_quote = false
in_code = false
spaces = 0
ARGF.each_line do |line|
line = line.chomp
dbg :input, line
case line
when /^\s*<.*>\s*$/i
# date
dbg :case, 1, :angle_date
next
when /^\s*:\s*(.*)/i
# code line
dbg :case, 2, :code_line, $1
puts code_block($1)
next
when /^\s*#\+BEGIN_QUOTE/i
# start quote
dbg :case, 3, :begin_quote
in_quote = true
next
when /^\s*#\+END_QUOTE/i
# end quote
dbg :case, 4, :end_quote
in_quote = false
next
when /^\s*#\+BEGIN_SRC/i
# start code block
dbg :case, 5, :begin_src
in_code = 0
spaces = 0
#dbg :BEGIN_SRC
start_code_block
next
when /^\s*#\+END_SRC/i
# end code block
dbg :case, 6, :end_src
in_code = false
spaces = 0
#dbg :END_SRC
end_code_block
next
when /^\s*#/i
# comment
dbg :case, 7, :comment
#dbg :IGNORE
# ignore
next
when /^\*+ !SLIDE/
# SLIDES
dbg :case, 8, :slide
#dbg :SLIDE
slide
next
when /^\*+/
# headings
dbg :case, 9, :heading
line = line.gsub(/^(\*+)(\s+.*$)/) do |m|
"\n\nh#{$1.size}.#{$2}\n\n"
end
when /^\|\-+/
dbg :case, 10, :table_header
# skip table header lines
next
when /^\s*\-+\s+/
# bullet list
dbg :case, 11, :bullet_list_item
line = line.gsub(/^(\s*\-)/) do |m|
"*" * (($1.size + 1) / 2)
end
end
# replace links
line = line.gsub(/\[\[(.*?)\]\]/){|match|
link, desc = $1.split(/\]\[/)
dbg [:match, match, :one, $1, :link, link, :desc, desc].inspect
link = expand_link(link)
if desc
"[#{desc}|#{link}]"
else
"[##{link}]"
end
}
# formatting
line = line.gsub(/\s\/(.+?)\/\s/){|match|
# make sure not inside link
" _#{$1}_ "
}
# bit crude but works (mostly)
line = line.gsub(/\s=(.*?)=/, ' {{\\1}}')
if in_code
dbg :case, :in_code
if in_code == 0
m = line.match(/^\s+/)
spaces = m.to_s.size
end
# p [:line, line, :spaces, spaces]
#if $confluence
puts line[spaces..-1]
#else
# puts (" " * 4) + line
#end
#puts "#{in_code}: #{line}"
in_code += 1
else
if in_quote
dbg :case, :in_quote
print "bq. "
end
dbg :output, line
puts line
end
end