Re: [AD] another web site proposal

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Tue, 2008-12-16 at 13:12 +1100, Peter Wang wrote:
> 
> It doesn't help so much people working on Windows where symlinks don't
> exist, or where lndir might be hard to get.  Probably the solution is to
> do like Elias says and write the scripts in Python one day.
> 

Well, if we want to use Python for the web generator scripts, I started
with a line-by-line translation of make_page. I can also translate the
other scripts - but not sure how it solves the symlink problem.

-- 
Elias Pschernig <elias@xxxxxxxxxx>
#!/usr/bin/env python
import os, glob, sys, re

TOP = os.getcwd()
PANDOC = os.getenv("PANDOC", "pandoc")
STYLESHEET = "web_style.css"

def check_for_pandoc():
    if os.system("which >/dev/null 2>&1 " + PANDOC):
        # Make things a little easier on SF.net servers.
        bin = "/home/groups/a/al/alleg/pandoc/bin"
        os.environ["PATH"] = os.environ["PATH"] + ":" + bin
        if os.system("which >/dev/null 2>&1 " + PANDOC):
            sys.stderr.write(PANDOC + " not found in PATH\n")

def get_page_files(path):
    found = glob.glob("inc.a.*")
    if os.path.isdir(path):
        found += revdatesort(glob.glob(os.path.join(path, "*")))
    else:
        found += [path]
    found += glob.glob("inc.z.*")
    return found

# Reverse any series of filenames containing dates.
def revdatesort(files):
    files.sort()
    datefiles = []
    result = []
    date_regexp = ".*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].*"
    for name in files:
        if re.match(date_regexp, name):
            datefiles.append(name)
        else:
            datefiles.reverse()
            result += datefiles
            result.append(name)
            datefiles = []
    
    result += datefiles
    return result

def get_title(page):
    for line in file(os.path.join(TOP, "en", "META.title")):
        if line.startswith(page + "="):
            return line.strip().split("=")[1]
    return ""

def main():
    if len(sys.argv) != 3:
        sys.stderr.write("usage: %s <input file> <output directory>\n"
            % sys.argv[0])
        sys.exit(1)

    check_for_pandoc()

    indir = os.path.dirname(sys.argv[1])
    inpage = os.path.basename(sys.argv[1])
    
    if sys.argv[2].startswith("/"):
        outdir = sys.argv[2]
    else:
        outdir = os.path.join(os.getcwd(), sys.argv[2])

    outpage = os.path.join(outdir, inpage + ".html")

    # Sanity check that don't overwrite an input file by passing wrong
    # args.
    if not "/OUT/" in outpage:
        sys.stderr.write("Refusing to write to ${outpage}.\n")
        sys.exit(2)

    if not os.path.exists(outdir):
        os.mkdir(outdir)

    os.chdir(indir)

    page_files = get_page_files(inpage)

    title = get_title(inpage)

    command = PANDOC
    for page in page_files:
        command += ' "%s"' % page
    params = ["--include-in-header", "inc.head",
        "--title-prefix", '"' + title + '"',
        "--css", '"' + STYLESHEET + '"',
        "--standalone",
        "--output", '"' + outpage + '"']
    command += " " + " ".join(params)
    os.system(command)

if __name__ == "__main__":
    main()

# vim: ft=sh sts=4 sw=4 et:


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/