#! /usr/bin/env python # -*- coding: ISO-8859-2 -*- # generate_index.py (27.11. 2003) -- generates 'index.html' from 'drd.txt' import sys import re LT_QUEST = 1 LT_STORY = 2 LT_OTHER = 3 DRD_TXT = "./drd.txt" INDEX_HTML = "./index.html" HEADER = """ Thorgalovy příběhy

Thorgalovy příběhy

Thorgalovy vlastnosti ]  [ Soubor stylů ]  [ drd.txt ]  [ generate_index.py ]

    """ FOOTER = "\n
\n\n\n" for_the_first_time = 1 def main(args): try: fin = open(DRD_TXT, "r") fout = open(INDEX_HTML, "w") fout.write(HEADER) line = fin.readline() while (line != ""): html_fragment = process_line(line) fout.write(html_fragment) line = fin.readline() fout.write(FOOTER) fout.close() fin.close() except Exception, err: print err; def process_line(line): fragment = "" (line_type, re_match) = guess_line_type(line) if (line_type == LT_QUEST): fragment = quest_html_fragment(re_match) elif (line_type == LT_STORY): fragment = story_html_fragment(re_match) else: pass return fragment def guess_line_type(line): quest_pattern = \ re.compile("^- - - -\s+([IVXLCDM]+)\.\s+((\w+\s?)+)+\s+- - - -$") story_pattern = \ re.compile("^([\*\+ ])?\s+(.+\.htm)\s+\[(.+)\]\s+--\s+((\w+).*)$") match = quest_pattern.search(line) if (match): return (LT_QUEST, match) match = story_pattern.search(line) if (match): return (LT_STORY, match) return (LT_OTHER, match) def quest_html_fragment(re_match): # \g<1> quest order # \g<2> quest name tmp = re_match.expand("\n
  • \g<2>\n