4 # Fixes htags' output so that files have fixed names. E.g., S/542.html -> D/whatever.c.h.
6 # It addresses the following problem:
8 # "Can htags create permanent addresses?"
9 # http://lists.gnu.org/archive/html/help-global/2015-11/msg00002.html
15 Encoding
.default_external
= "BINARY"
17 $base = '.' # Where the HTML is stored.
20 $create_subdirs = false # Experimental feature. Doesn't really work.
22 # Bonus: highlight the target line.
36 # Computes pretty names for files:
38 # '.../S/456.html' => 'S/modules/tty.c.html'
40 def prettyname(full_path
, prefix
, kind
, nid
, ext
)
42 start
= IO
.read(full_path
, 1024)
43 if start
=~
/<title>(.*?)<\/title
>/ then
45 gist
.gsub
!(%r
{/+$}, '') # remove trailing '/'s.
46 if not $create_subdirs then
49 gist.gsub!(%r{[^a-zA-Z0-9_.,/-]}, '-') # clean special characters.
51 return kind + '/' + gist + ext
63 print "[#{@count}/#{@total}]\r"
73 # Returns a table explaining how to rename the files. E.g.:
76 # 'S
/456.html' => 'S/modules
/tty
.c
.html
'
83 Dir[$base + '/{[A-Z],files}/*.html
'].each do |full_path|
85 if full_path =~ /(.*)\/((\w+)\/(\d+)(\.\w+))$/ then
86 prefix = $1 # "/home/joe/.../HTML"
87 path = $2 # "S/323.html"
91 pretty = prettyname(full_path, prefix, kind, nid, ext)
101 # Make all the substiutions inside the files:
103 # <a href='../S/456.html
'> --> <a href='../S/modules
/tty
.c
.html
'>
108 Dir[$base + '/**/*.html
'].each do |path|
109 kind = path[$base.length .. -1][/\w+/]
111 fixed = text.gsub(/(<a href='(?:..\
/)?)([^
'#]+)/) do |a|
116 elsif cnv[kind + '/' + target] then
117 # This is a relative link of the form: href='456.html
'
118 if cnv[kind + '/' + target].match /\
/(.*)/ # converts "s/whatever.html" to "whatever.html"
125 # Fix a problem in Opera, where empty lines are squashed.
126 fixed
.gsub
!(%r
{(<a id
='L\d+' name
='L\d+'></a
>)\n}, "\\1 \n")
128 if text
!= fixed
then
129 IO
.write(path
, fixed
)
130 puts(path
+ " modified") if $debug
143 src
= $base + '/' + a
144 trg
= $base + '/' + b
145 if $create_subdirs then
146 FileUtils
.mkdir_p(File
.dirname(trg
))
148 File
.rename(src
, trg
)
154 def better_css(css_path
)
155 text
= IO
.read(css_path
)
156 IO
.write(css_path
, text
+ $css)
160 if not File
.exists
?($base + '/help.html') then
162 if not File
.exists
?($base + '/help.html') then
163 puts
"Error: This doesn't look like htags' output directory."
167 Counter
.init( Dir
[$base + '/**/*.html'].length
)
168 puts
"Figuring out pretty names..."
169 cnv
= build_cnv_tbl()
171 puts
"Updating links in files..."
173 puts
"Renaming files..."
175 puts
"Enhancing the css..."
176 better_css($base + '/style.css')