-
Notifications
You must be signed in to change notification settings - Fork 15
/
md-preprocessor.rb
78 lines (65 loc) · 1.52 KB
/
md-preprocessor.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
#encoding:utf-8
require 'pp'
module MarkdownPP
DirectivePattern = /\#\{&([:=.?]+)(.*?)\}/
DirectiveHandlers = Hash[*%w|~ translator_added = alternative ? undecided := may_change .. unfinished < tag_begin > tag_end|]
MayChange = {}
Undecided = []
Unfinished = []
Blank = ''
module_function
def scan_pp_directives(lines)
lines.each_with_index do |line,lineno|
for directive, params in line.scan DirectivePattern
if directive == ':=' and not params.nil?
english, value = params.split ','
MayChange[english.strip] = value unless value.nil?
elsif directive == '?'
Undecided << [lineno, line, params]
elsif directive == '..'
Unfinished << [lineno, line]
end
end
end
end
def sub_pp_directives(lines)
lines.map do |line|
line = line.gsub(DirectivePattern) do
method = DirectiveHandlers[$1]
method.nil? ? Blank : self.send(method.to_sym,*$2.split(','))
end
line.gsub(/\#\{(.*?)\}/) do
MayChange[$1]
end
end
end
def parse(text)
scan_pp_directives text
sub_pp_directives text
end
def may_change(english, value = nil)
MayChange[english]
end
def undecided(english, worst_choice = nil)
worst_choice || Blank
end
def unfinished(why = nil)
"[Unfinished,#{why}]"
end
def translator_added(*sentence)
"(#{sentence})"
end
def alternative(alt)
"(#{alt})"
end
def tag_begin(tag)
"<#{tag}>"
end
def tag_end(tag)
"</#{tag}>"
end
def self.method_missing(m,p)
Blank
end
end
MarkdownPP.parse(File.readlines("#{File.realpath(ARGV.first)}"))