]> git.donarmstrong.com Git - lilypond.git/blobdiff - stepmake/bin/add-html-footer.py
`bug'fix: a colon `:' is added (only) after a sentence which is complete;
[lilypond.git] / stepmake / bin / add-html-footer.py
index adb115208822fc8537e15520a52a258589652730..18b691719b72388489aadf8abc539851299a5a82 100644 (file)
@@ -10,8 +10,9 @@ import time
 import string 
 import getopt
 
-fullname = "unknown"
-index_file=''
+gcos = "unknown"
+index_url=''
+top_url=''
 changelog_file=''
 package_name = ''
 package_version = ''
@@ -33,15 +34,16 @@ footer_file = ''
 default_header = r"""
 """
 
-default_footer = r"""<hr>Please take me <a href=%s>back to the index</a>
-of %s
-<!-- package name %s>
- <!-- webmaster fields. %s %s>
+default_footer = r"""<hr>Please take me <a href=@INDEX@>back to the index</a>
+of @PACKAGE_NAME@
 """
 
-builtstr = r"""<hr><font size=-1>
-This page was built from %s-%s by 
-<address><br>%s &lt<a href="mailto:%s">%s</a>&gt,  %s.</address><p></font>"""
+built = r"""<hr>
+<p><font size="-1">
+This page is for @PACKAGE_NAME@-@PACKAGE_VERSION@ (@BRANCH@). <br>
+</font>
+<address><font size="-1">
+Report errors to &lt;<a href="mailto:@MAILADDRESS@">@MAILADDRESS@</a>&gt;.</font></address>"""
 
 
 def gulp_file (f):
@@ -60,7 +62,7 @@ def gulp_file (f):
        return s
 
 def help ():
-       sys.stdout.write (r"""Usage: add-html-footer [OPTION]... HTML-FILE
+       sys.stdout.write (r"""Usage: add-html-footer [OPTIONS]... HTML-FILE
 Add header, footer and top of ChangLog file (up to the ********) to HTML-FILE
 
 Options:
@@ -71,6 +73,7 @@ Options:
   --index=URL               set homepage to URL
   --name=NAME               set package_name to NAME
   --version=VERSION         set package version to VERSION
+
 """)
        sys.exit (0)
 
@@ -89,8 +92,9 @@ for opt in options:
                header_file = a
        elif o == '-h' or o == '--help':
                help ()
+       # urg, this is top!
        elif o == '--index':
-               index_file = a
+               index_url = a
        elif o == '--name':
                package_name = a
        elif o == '--version':
@@ -98,48 +102,91 @@ for opt in options:
        else:
                raise 'unknown opt ', o
 
-def set_vars ():
-       global fullname
+#burp?
+def set_gcos ():
+       global gcos
        os.environ["CONFIGSUFFIX"] = 'www';
        if os.name == 'nt':
                import ntpwd
                pw = ntpwd.getpwname(os.environ['USERNAME'])
        else:
                import pwd
-               pw = pwd.getpwuid (os.getuid());
+               if os.environ.has_key('FAKEROOTKEY') and os.environ.has_key('LOGNAME'):
+                       pw = pwd.getpwnam (os.environ['LOGNAME'])
+               else:
+                       pw = pwd.getpwuid (os.getuid())
 
        f = pw[4]
        f = string.split (f, ',')[0]
-       fullname = f 
-
-#burp
-def compose_header ():
-       global default_header
-       head = default_header
-       if header_file:
-               head = gulp_file (header_file)
-       return head
-
-def compose_footer (index):
-       global default_footer
-       foot = default_footer
-
-       if footer_file:
-               foot = gulp_file (footer_file)
-
-       s = foot % (index, package_name, package_name, webmaster, webmaster)
-       s = s + builtstr % (package_name, package_version, fullname,
-                           mail_address, mail_address, 
-                           time.strftime ('%c %Z', time.localtime (time.time ())))
+       gcos = f 
+
+def compose (default, file):
+       s = default
+       if file:
+               s = gulp_file (file)
        return s
 
-set_vars ()
-header = compose_header ()
-footer = compose_footer (index_file)
-header_tag = '<! header_tag >'
-footer_tag = '<! footer_tag >'
+set_gcos ()
+localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
+
+if os.path.basename (index_url) != "index.html":
+       index_url = os.path.join (index_url , "index.html")
+top_url = os.path.dirname (index_url) + "/"
+
+header = compose (default_header, header_file)
+
+# compose (default_footer, footer_file)
+footer =  built
+header_tag = '<!-- header_tag -->'
+footer_tag = '<!-- footer_tag -->'
+
+# Python < 1.5.2 compatibility
+#
+# On most platforms, this is equivalent to
+#`normpath(join(os.getcwd()), PATH)'.  *Added in Python version 1.5.2*
+if os.path.__dict__.has_key ('abspath'):
+       abspath = os.path.abspath
+else:
+       def abspath (path):
+               return os.path.normpath (os.path.join (os.getcwd (), path))
+
+
+def remove_self_ref (s):       
+       self_url = abspath (os.getcwd () + '/' + f)
+       #sys.stderr.write ('url0: %s\n' % self_url)
+
+       # self_url = re.sub ('.*?' + string.lower (package_name) + '[^/]*/',
+       #                '', self_url)
+       # URG - this only works when source tree is unpacked in `src/' dir
+       # For some reason, .*? still eats away
+       #     /home/fred/usr/src/lilypond-1.5.14/Documentation/user/out-www/lilypond/
+       # instead of just
+       #
+       #     /home/fred/usr/src/lilypond-1.5.14/
+       #
+       #     Tutorial.html
+       self_url = re.sub ('.*?src/' + string.lower (package_name) + '[^/]*/',
+                        '', self_url)
+
+       #sys.stderr.write ('url1: %s\n' % self_url)
+       
+       #urg, ugly lily-specific toplevel index hack
+       self_url = re.sub ('.*topdocs/out-www/index.html', 'index.html', self_url)
+       #sys.stderr.write ('url2: %s\n' % self_url)
+
+       # ugh, python2.[12] re is broken.
+       ## pat = re.compile ('.*?(<a href="[\./]*' + self_url + '#?[^"]*">)([^<]*)(</a>)', re.DOTALL)
+       pat = re.compile ('[.\n]*?(<a href="[\./]*' + self_url + '#?[^"]*">)([^<]*)(</a>)')
+       m = pat.search (s)
+       while m:
+               #sys.stderr.write ('self: %s\n' % m.group (2))
+               s = s[:m.start (1)] + m.group (2) + s[m.end (3):]
+               m = pat.search (s)
+       return s
+
+def do_file (f):
+       s = gulp_file (f)
 
-def do_file (s):
        if changelog_file:
                changes = gulp_file (changelog_file)
                # urg?
@@ -147,49 +194,91 @@ def do_file (s):
                m = re.search (r'\*\*\*', changes)
                if m:
                        changes = changes[:m.start (0)]
-               s = re.sub ('top_of_ChangeLog', '<XMP>\n'+ changes  + '\n</XMP>\n', s)
+               s = re.sub ('top_of_ChangeLog', '<pre>\n'+ changes  + '\n</pre>\n', s)
 
        if re.search (header_tag, s) == None:
-               body='<BODY BGCOLOR=WHITE><FONT COLOR=BLACK>'
+               body = '<BODY BGCOLOR=WHITE TEXT=BLACK>'
                s = re.sub ('(?i)<body>', body, s)
                if re.search ('(?i)<BODY', s):
-                       s = re.sub ('(?i)<body[^>]*>', body + header, s)
+                       s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
                elif re.search ('(?i)<html', s):                
-                       s = re.sub ('(?i)<html>', '<HTML>' + header, s)
+                       s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
                else:
                        s = header + s
 
-               s = header_tag + s
+               s = header_tag + '\n' + s
+
+               if re.search ('(?i)<!DOCTYPE', s) == None:
+                       doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
+                       s = doctype + s
 
        if re.search (footer_tag, s) == None:
-               s = s + footer_tag
+               s = s + footer_tag + '\n'
 
                if re.search ('(?i)</body', s):
-                       s = re.sub ('(?i)</body>', footer + '</BODY>', s)
+                       s = re.sub ('(?i)</body>', footer + '</BODY>', s, 1)
                elif re.search ('(?i)</html', s):               
-                       s = re.sub ('(?i)</html>', footer + '</HTML>', s)
+                       s = re.sub ('(?i)</html>', footer + '</HTML>', s, 1)
                else:
                        s = s + footer
 
-       return s
 
+       #URUGRGOUSNGUOUNRIU
+       index = index_url
+       top = top_url
+       if os.path.basename (f) == "index.html":
+               cwd = os.getcwd ()
+               if os.path.basename (cwd) == "topdocs":
+                       index = "index.html"
+                       top = ""
+
+               # don't cause ///////index.html entries in log files.
+               #       index = "./index.html"
+               #       top = "./"
+
+
+       versiontup = string.split(package_version, '.')
+       branch_str = 'stable-branch'
+       if string.atoi ( versiontup[1]) %  2:
+               branch_str = 'development-branch'
+               
+       s = re.sub ('@INDEX@', index, s)
+       s = re.sub ('@TOP@', top, s)
+       s = re.sub ('@PACKAGE_NAME@', package_name, s)
+       s = re.sub ('@PACKAGE_VERSION@', package_version, s)
+       s = re.sub ('@WEBMASTER@', webmaster, s)
+       s = re.sub ('@GCOS@', gcos, s)
+       s = re.sub ('@LOCALTIME@', localtime, s)
+       s = re.sub ('@MAILADDRESS@', mail_address, s)
+       s = re.sub ('@BRANCH@', branch_str, s)  
+
+       # ugh, python2.[12] re is broken.
+       #pat = re.compile ('.*?<!--\s*(@[^@]*@)\s*=\s*([^>]*)\s*-->', re.DOTALL)
+       pat = re.compile ('[.\n]*?<!--\s*(@[^@]*@)\s*=\s*([^>]*)\s*-->')
+       m = pat.search (s)
+       while m:
+               at_var = m.group (1)
+               at_val = m.group (2)
+               sys.stderr.write ('at: %s -> %s\n' % (at_var, at_val))
+               s = re.sub (at_var, at_val, s)
+               m = pat.search (s)
+
+       # urg
+       # maybe find first node?
+       fallback_web_title = '-- --'
+
+       # ugh, python2.[12] re is broken.
+       #m = re.match ('.*?<title>\(.*?\)</title>', s, re.DOTALL)
+       m = re.match ('[.\n]*?<title>([.\n]*?)</title>', s)
+       if m:
+               fallback_web_title = m.group (1)
+       s = re.sub ('@WEB-TITLE@', fallback_web_title, s)
+       
+       s = remove_self_ref (s)
 
-for f in files:
-       s = gulp_file (f)
-       s = do_file (s)
        open (f, 'w').write (s)
 
-if 0:
-       title = '<HEAD><TITLE>' \
-               + package_name + ' -- ' + os.path.basename (os.path.splitext(f)[0]) \
-               + '</TITLE></HEAD>'
-       s = check_tag ('<title>', title, s, 0)
-
-       s = check_tag ('<html', '', s, 0)
-       if regex.search ('<HTML', s) == -1:
-               s = '<HTML>\n' + s
-       s = check_tag ('</html>', '</HTML>', s, 1)
-
-       dump_file (f, s)
 
+for f in files:
+       do_file (f)