-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompile-rd.rb
executable file
·109 lines (101 loc) · 2.33 KB
/
compile-rd.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
#!/usr/bin/env ruby
if ARGV.size < 2
puts "Usage: #{$0} RD COMPILED_RD [LANG]"
exit 1
end
rd, rdc, lang, = ARGV
require 'English'
require 'cairo'
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'cairo-dummy'
require 'rd-lib'
require 'i18n'
load_message(lang)
section = nil
cairo_signature_re =
/\b?Cairo(?:::[A-Z][\w\d_]+)*(?:[#.][\[_@\w\d]*(?:[=?!_*+\-\]]|\b)?|\b)?/
in_pre = false
auto_linked_rd = File.open(rd).collect do |line|
case line
when /^#\s*(\S*)\s*/ #
pragma = $1
case pragma
when "start-pre"
in_pre = true
when "end-pre"
in_pre = false
end
line
when /^---/
line
when /^=+\s+(.*)\s+$/
section = $1
if /\A(class|module) (.+)\z/ =~ section
type, klass = $1, $2
line.gsub(section, _("#{type} %s") % klass)
else
line.gsub(section, _(section))
end
else
line = line.gsub(/\*\s*(Returns|Block returns)\s*:\s*/) do
"* #{_($1)}: "
end
line.gsub(/(\(\(<(?:[^|]+\|)?)?(#{cairo_signature_re}|Index)/) do |signature|
link_markup = $1
if link_markup or section == "Object Hierarchy" or in_pre
signature
else
"((<#{signature}>))"
end
end
end
end.join
def resolve_link(link_content)
case link_content
when /\./
klass_name = $PREMATCH
method_name = $POSTMATCH
klass = eval(klass_name)
RDLib.class_method_link(klass, method_name)
when /#/ #
klass_name = $PREMATCH
method_name = $POSTMATCH
klass = eval(klass_name)
RDLib.instance_method_link(klass, method_name)
when "Cairo", /::/
klass = eval(link_content)
if klass.is_a?(Module)
RDLib.class_link(klass)
else
components = link_content.split(/::/)
const = components.pop
klass = eval(components.join("::"))
RDLib.constant_link(klass, const)
end
else
nil
end
end
compiled_rd = auto_linked_rd.gsub(/\(\(<(.*?)>\)\)/) do |link|
link_content = $1
case link_content
when "Index"
RDLib.index_link(_("Index"))
when /URL:/
link
when /\|/
label = $PREMATCH
link_content = $POSTMATCH
resolved_link = resolve_link(link_content)
if resolved_link.nil?
link
else
resolved_link.sub(/\A(\(\(<)(?:[^|]+\|)?/, "\\1#{label}|")
end
else
resolve_link(link_content) || link
end
end
File.open(rdc, "w") do |output|
output.print compiled_rd
end