#!/usr/bin/env python3.0 # -*- coding: utf-8 -*- import os import re import cgi import string menu_order = ["Symbols", "Sources"] #menu_order = ["Symbols", "Grammars", "Sources"] menu_order.reverse() #Yeah. Reversed. menu = { "Grammars": 'TODO: List of other formatted grammars go here', "Sources":""" Written by purpleposeidon """, "Symbols":"""

Meaning

A B
concatenation
grammatical-construct
invokes another rule
TERMINAL
a selma'o
[ A ]
optional
...
repeat previous construct zero or more
A | B
alternation
A & C
and/or
#
[free ...]
/ A /
contents only required if changes the parse tree

Precedence

  1. ...
  2. &
  3. |
  4. concatenation
""", } def htmlify(src, dest): def highlight_words(val): breaks = "[](). \n&/#" sep = '\0' for b in breaks: val = val.replace(b, sep+b+sep) val = val.split(sep) r = '' for v in val: if not v: continue if v in ('CMENE', 'BRIVLA'): v = '{1}'.format(v.lower(), v) elif v.replace('h', 'H') == v.upper() and set(v).issubset(set(string.ascii_letters)): #Is a selma'o v = '{0}'.format(v) elif v in list_order: #Is a grammar thingie v = '{0}'.format(v) r += v return r grammar = {} dess = {} list_order = [] defs = [] key = None header = '' descr = '' for line in open(src): if line[0] == ';': line = line[1:] if line and line[0] == ';': continue #Double ;; == ignore if key == None: if header: descr += line else: header = line else: dess[key] += line continue if line[0] == ' ': if ';' in line: line, com = line.split(';', 1) if com and com[0] != ';': dess[key] += cgi.escape(com) line = cgi.escape(line) defs[-1] += line grammar[key] += line elif '=' in line: line = cgi.escape(line) defs.append(line) key, des = line.split('=') key = key.strip() des = des.strip() if des and des[0] == ';': des = des[1:] grammar[key] = '' dess[key] = des list_order.append(key) len_order = list(list_order) len_order.sort(key=lambda i: -len(i)) out = open(dest, 'w') out.write(""" Lojban EBNF """) if header: out.write("

%s

"%header) if descr: out.write("

%s

"%descr) if menu: out.write("""\n""") for key in list_order: val = grammar[key] pairs = [("[", "]"), ('(', ')')] val = highlight_words(val) for A, B in pairs: #val = val.replace() val = val.replace(A, ''+A) val = val.replace(B, B+'') val = val.replace(' ', '') if dess[key]: desc = '
{0}
'.format(dess[key]) else: desc = '' out.write("""
{2} {0} =
{1}
""".format(key, val, desc)) out.write(""" """) if "REQUEST_URI" in os.environ: print("Content-type: text/x-python") print("Content-disposition: attatchment; filename=format.py") print() print(open("format.py").read()) for args in [("lojban.bnf", "index.html"), ("wiki.bnf", "djeims.html")]: #import sys #print(args, file=sys.stderr) htmlify(*args)