Add syntax highlighting.
authorGlenn Hutchings <zondo42@gmail.com>
Thu, 31 Mar 2016 18:59:44 +0000 (19:59 +0100)
committerGlenn Hutchings <zondo42@gmail.com>
Thu, 31 Mar 2016 18:59:44 +0000 (19:59 +0100)
.hgignore
README.md
about.rst
chapters/01.rst
conf.py
tools/inform.py [new file with mode: 0644]
tools/transcript.py [new file with mode: 0644]

index aea98ed774aa5a592c3d5acf32bc0610012ba1be..aac148802f5565d84721a6d1f5135f075300b983 100644 (file)
--- a/.hgignore
+++ b/.hgignore
@@ -1,3 +1,5 @@
 syntax: glob
 
 output/
+
+*.pyc
index e3f57731a6efb64ce62a4b5c675d09d5cbd7104d..6c0485f6718730ec3b88c6b23cd14f7d9ad934bf 100644 (file)
--- a/README.md
+++ b/README.md
@@ -66,5 +66,4 @@ others.  Also, in parallel with that, other niceties:
 
 * Add a glossary
 * Add an index
-* Add Inform syntax highlighting
 * Prepare a new PDF version, via the Sphinx LaTeX converter
index 13b4b0b4fe44b69ec0d2495d0b0b287045232191..84c443718842cac287cda73f83b0c56503b386ac 100644 (file)
--- a/about.rst
+++ b/about.rst
@@ -2,6 +2,8 @@
  About this guide
 ==================
 
+.. highlight:: inform
+
 .. epigraph::
 
    | *If they asked me, I could write a book;*
index ece83a92d6754af400ed58250f444e6d0c70c725..958000f17d04d66259774ef2db2f41fba464ab7e 100644 (file)
@@ -2,6 +2,8 @@
  1: Just what is interactive fiction?
 ======================================
 
+.. highlight:: transcript
+
 .. epigraph::
 
    | *A was an archer, who shot at a frog;*
diff --git a/conf.py b/conf.py
index 5f579c0d4074f5ef95aed9486aa896de1e6ed802..0e3fb332da64b361f2d6c017bb81aa7170dee1df 100644 (file)
--- a/conf.py
+++ b/conf.py
@@ -19,7 +19,15 @@ import shlex
 # If extensions (or modules to document with autodoc) are in another directory,
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
-#sys.path.insert(0, os.path.abspath('.'))
+sys.path.insert(0, os.path.abspath('tools'))
+
+from inform import InformLexer
+from transcript import TranscriptLexer
+
+# Setup function.
+def setup(app):
+    app.add_lexer('inform', InformLexer())
+    app.add_lexer('transcript', TranscriptLexer())
 
 # -- General configuration ------------------------------------------------
 
@@ -92,7 +100,7 @@ default_role = "any"
 #show_authors = False
 
 # The name of the Pygments (syntax highlighting) style to use.
-pygments_style = 'sphinx'
+pygments_style = 'default'
 
 # The default Pygments highlight language.
 highlight_language = 'none'
diff --git a/tools/inform.py b/tools/inform.py
new file mode 100644 (file)
index 0000000..63d098b
--- /dev/null
@@ -0,0 +1,94 @@
+"""
+Inform syntax highlighting.
+
+TODO: get it working!
+"""
+
+from pygments.lexer import RegexLexer
+from pygments.token import (Text, Comment, Operator, Keyword, Name,
+                            String, Number, Punctuation, Token)
+
+objects = ["Class", "Nearby", "Object"]
+
+directives = ["Abbreviate", "Array", "Attribute", "Btrace", "Class",
+              "Constant", "Default", "Dictionary", "End", "Endif", "Etrace",
+              "Extend", "Fake_action", "Global", "Ifdef", "Iffalse",
+              "Ifndef", "Ifnot", "Iftrue", "Ifv3", "Ifv5", "Import",
+              "Include", "Link", "Listsymbols", "Listdict", "Listverbs",
+              "Lowstring", "Ltrace", "Message", "Nearby", "Nobtrace",
+              "Noetrace", "Noltrace", "Notrace", "Object", "Property",
+              "Release", "Replace", "Serial", "Statusline", "Stub",
+              "Switches", "System_file", "Trace", "Verb", "Version",
+              "Zcharacter"]
+
+defining = ["[", "array", "attribute", "class", "constant", "fake_action",
+            "global", "lowstring", "nearby", "object", "property"]
+
+attributes = ["absent", "animate", "clothing", "concealed", "container",
+              "door", "edible", "enterable", "female", "general", "light",
+              "lockable", "locked", "male", "moved", "neuter", "on", "open",
+              "openable", "pluralname", "proper", "scenery", "scored",
+              "static", "supporter", "switchable", "talkable", "transparent",
+              "visited", "workflag", "worn"]
+
+properties = ["n_to", "s_to", "e_to", "w_to", "ne_to", "se_to", "nw_to",
+              "sw_to", "u_to", "d_to", "in_to", "out_to", "add_to_scope",
+              "after", "article", "articles", "before", "cant_go", "capacity",
+              "daemon", "describe", "description", "door_dir", "door_to",
+              "each_turn", "found_in", "grammar", "initial",
+              "inside_description", "invent", "life", "list_together",
+              "name", "number", "orders", "parse_name", "plural",
+              "react_after", "react_before", "short_name", "short_name_indef",
+              "time_left", "time_out", "when_closed", "when_open", "when_on",
+              "when_off", "with_key"]
+
+keywords = ["box", "break", "continue", "do", "else", "font off", "font on",
+            "for", "give", "has", "hasnt", "if", "in", "inversion", "jump",
+            "move", "new_line", "notin", "objectloop", "ofclass", "or",
+            "print", "print_ret", "provides", "quit", "read", "remove",
+            "restore", "return", "rfalse", "rtrue", "save", "spaces",
+            "string", "style bold", "style fixed", "style reverse",
+            "style roman", "style underline", "switch", "to", "until",
+            "while"]
+
+action_re = r"\s-*\*"
+
+def wordlist(list):
+    return "(" + "|".join(list) + r")\b"
+
+class InformLexer(RegexLexer):
+    """
+    Inform code lexer.
+    """
+
+    name = 'Inform'
+    aliases = ['inform']
+    filenames = ['*.inf']
+    mimetypes = ['text/x-inform', 'application/x-inform']
+
+    tokens = {
+        'root': [
+            (r'\n', Text),
+            (r'[^\S\n]+', Text),
+            (r'!.*$', Comment.Single),
+            (r'\\\n', Text),
+            (r'\\', Text),
+            (r'=', Operator),
+            (r'"[^"]*"', String.Double),
+            (r"'[^']*'", String.Single),
+            (r"\S+:", Name.Label),
+            (r"<<.+>>", Name.Label),
+
+            (wordlist(objects), Name.Class),
+            (wordlist(keywords), Token.Keyword.Reserved),
+            (wordlist(properties), Name.Builtin),
+            (wordlist(directives), Name.Entity),
+            (wordlist(attributes), Name.Attribute),
+
+            (r'[a-zA-Z_][a-zA-Z0-9_.]*', Name),
+            (r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
+            (r'\d+', Number.Integer),
+
+            (r'.', Punctuation),
+        ],
+    }
diff --git a/tools/transcript.py b/tools/transcript.py
new file mode 100644 (file)
index 0000000..8fe45ce
--- /dev/null
@@ -0,0 +1,29 @@
+"""
+IF transcript syntax highlighting.
+"""
+
+import re
+
+from pygments.lexer import RegexLexer
+from pygments.token import Generic, Text
+
+class TranscriptLexer(RegexLexer):
+    """
+    IF transcript lexer.
+    """
+
+    name = 'Interactive Fiction transcript'
+    aliases = ['transcript']
+    filenames = ['*.scr']
+    mimetypes = ['text/x-scr', 'application/x-scr']
+
+    tokens = {
+        'root': [
+            (r'\n', Text),
+            (r'^[^>].*', Text),
+            (r'^>', Text, 'command'),
+        ],
+        'command': [
+            (r'.*', Generic.Strong, '#pop'),
+        ],
+    }