Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 153 lines (138 sloc) 4.098 kb
d9b079a Initial revision
tim authored
1 # setup RMagick documentation
2
3 require 'ftools'
4 require 'tempfile'
5
6 def err(msg)
7 $stderr.puts "#{$0}: #{msg}"
8 exit
9 end
10
11 # Where to find the ImageMagick documentation
12 def imbaseuri
13 get_config('imdoc-base-uri')
14 end
15
16 # Various methods to get info from the install.rb config table
17 def rubyprog
18 get_config('ruby-prog')
19 end
20
21 # Allow examples to fail?
22 def allow_example_errors
23 get_config('allow-example-errors') == 'yes'
24 end
25
26 # Edit a file in place, replacing all instances of the re "targ" with the string "rep"
27 def edit_in_place(src, targ, rep)
28 File.open('./rmagick.tmp', 'w') do |tmp|
29 File.open(src) do |s|
30 s.each do |line|
31 tmp.puts line.gsub(targ, rep)
32 end
33 end
34 end
35 File.mv('./rmagick.tmp', src)
36 end
37
38 # Edit links to ImageMagick documentation.
39 def edit_html
40 cwd = srcdir()
41 begin
42 Dir.chdir('doc')
43 Dir['*.html'].each { |file|
44 next if file =~ /\.rb.html/
45 # puts "post-setup.rb: setting up #{file}"
46 # include leading quote in regexp so only the href= attribute values are replaced
47 edit_in_place(file, /"http:\/\/www.imagemagick.org/, "\"#{imbaseuri()}")
48 }
49 ensure
50 Dir.chdir(cwd)
51 end
52 end
53
54 # Change shebang line to use path to installed ruby
55 def edit_shebang(example)
56 # puts "post-setup.rb: editing #{example}"
57 edit_in_place(example, /\A\#!\s*\S*ruby\s/, '#!'+rubyprog()+' ')
58 File.chmod(0755, example)
59 end
60
61 # Create HTML version of a Ruby example in doc directory.
62 def copy_to_html(example)
63 target = '../'+example+'.html'
64 return if FileTest.exist? target
65 # puts "post-setup.rb: setting up ../#{example}.html"
66 File.open(example) do |src|
67 File.open(target, 'w') do |dest|
68 dest.puts <<END_EXHTMLHEAD
69 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
70 <HTML>
71 <HEAD>
72 <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
73 <TITLE>Example: #{example}</TITLE>
74 </HEAD>
75 <BODY style=\"background-color: #fffff0;\">
76 <PRE>
77 END_EXHTMLHEAD
78 src.each do |line|
79 line.gsub!(/&/,'&amp;')
80 line.gsub!(/>/,'&gt;')
81 line.gsub!(/</,'&lt;')
82 dest.puts line
83 end
84 dest.puts <<END_EXHTMLTAIL
85 </PRE>
86 </BODY>
87 </HTML>
88 END_EXHTMLTAIL
89 end
90 File.chmod(0644, target)
91 end
92 end
93
94 def make_example_html
95 cwd = srcdir()
96 begin
97 Dir.chdir('doc/ex')
98 Dir['*.rb'].each { |file|
99 edit_shebang(file)
100 copy_to_html(file)
101 }
102 ensure
103 Dir.chdir(cwd)
104 end
105 end
106
107 # Add ./lib and ./ext/RMagick to $LOAD_PATH so that the examples are
108 # run using built RMagick.rb and RMagick.so, not the installed files.
109 # Gentoo installs into a temporary directory that is not in $LOAD_PATH.
110 def gen1_image(n, of, file)
111 puts("post-setup.rb: run #{file} example (#{n} of #{of} examples)")
112 system(rubyprog(), '-I', srcdir()+'/lib', '-I', srcdir()+'/ext/RMagick', file)
113 $?
114 end
115
116 # Generate the example images
117 def gen_example_images
118 errs = 0
119 exn = 0
120 cwd = srcdir()
121 begin
122 Dir.chdir('doc/ex')
123 examples = Dir['*.rb'].sort
124 nex = examples.length
125 examples.each do |file|
126 exn += 1
127 rc = gen1_image(exn, nex, file)
128 if rc != 0
129 puts("post-setup.rb: #{file} example returned error code #{rc}")
130 errs += 1 unless allow_example_errors()
131 if errs > 4
132 err(<<END_EXFAIL
133 Too many examples failed. The RMagick documentation cannot be installed
134 successfully. Consult the README.txt file and try again, or send email
135 to cyclists@nc.rr.com.
136 END_EXFAIL
137 )
138 end
139 end
140 end
141 ensure
142 Dir.chdir(cwd)
143 end
144 end
145
146 puts "\npost-setup.rb: setting up documentation..."
147
148 # No use doing this if base URI is the default.
149 edit_html unless imbaseuri() == "http://www.imagemagick.org"
150 make_example_html
151 gen_example_images
152 exit
Something went wrong with that request. Please try again.