Commits

Brian Mearns  committed 99acd2c

Added more to tutorial, and added more escape character macros (all of them from C), except for ?

  • Participants
  • Parent commits 1dedb8b

Comments (0)

Files changed (4)

File templ/tbuiltin.py

 class xLnbrk(texec.TMacro):
     """
     {lnbrk}
-		Aliases: "\n"
+		Aliases: "\\n"
 		: Line-break macro. Macro expands directly to the single-character
 		: String with ASCII value 0x0A (decimal 10). This is the traditional
 		: "linebreak" character, often escaped in other programming languages 
         return ttypes.String("\n")
 
 @texec.macro
-class xLnbrk(texec.TMacro):
-    """
-	x {tab}
-		Aliases: "\t"
+class xTab(texec.TMacro):
+    """
+	{tab}
+		Aliases: "\\t"
 		: Tab macro. Macro expands directly to the single-character
-		: String with ASCII value  . This is the traditional
+		: String with ASCII value 9. This is the traditional
 		: "horizontal tab" character, often escaped in other programming languages 
 		: as "\t". Note that templ has no escapes in strings or string
 		: literals.
         self.checkArgCount(name, args, exact=[0,])
         return ttypes.String("\t")
 
+@texec.macro
+class xBackslash(texec.TMacro):
+    """
+	{backslash}
+		: Backslash macro. Macro expands directly to the single-character
+		: backslash String.
+    """
+    __mnemonics__ = ("bslash", "\\")
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\\")
+
+@texec.macro
+class xDquote(texec.TMacro):
+    """
+	{dquote}
+		: Double-quote macro. Macro expands directly to the single-character
+		: String containing a double-quote character.
+    """
+    __mnemonics__ = ("double-quote", "dq", )
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\"")
+
+@texec.macro
+class xSquote(texec.TMacro):
+    """
+	{squote}
+		: Single-quote macro. Macro expands directly to the single-character
+		: String containing a single-quote character.
+    """
+    __mnemonics__ = ("single-quote", "sq", "\\'")
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("'")
+
+@texec.macro
+class xAlert(texec.TMacro):
+    """
+	{alert}
+		: Alert/Bell character macro. Macro expands directly to the single-character
+		: String containing an alert (aka BEL) character, ASCII value 7.
+    """
+    __mnemonics__ = ("alert", "bel", "bell", "\\a", )
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\a")
+
+@texec.macro
+class xBackspace(texec.TMacro):
+    """
+	{backspace}
+		: Backspace character macro. Macro expands directly to the single-character
+		: String containing a backspace character, ASCII value 8.
+    """
+    __mnemonics__ = ("bs", "\\b",)
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\b")
+
+@texec.macro
+class xFormfeed(texec.TMacro):
+    """
+	{formfeed}
+		: Formfeed character macro. Macro expands directly to the single-character
+		: String containing a formfeed character, ASCII value 12 (0x0C).
+    """
+    __mnemonics__ = ("ff", "\\f",)
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\f")
+
+@texec.macro
+class xCarriageReturn(texec.TMacro):
+    """
+	{carriage-return}
+		: CR character macro. Macro expands directly to the single-character
+		: String containing a carriage-return character, ASCII value 13 (0x0D).
+    """
+    __mnemonics__ = ("cr", "\\r",)
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\r")
+
+@texec.macro
+class xVerticalTab(texec.TMacro):
+    """
+	{vertical-tab}
+		: Vertical tab character macro. Macro expands directly to the single-character
+		: String containing a vertical-tab character, ASCII value 11 (0x0B).
+    """
+    __mnemonics__ = ("vt", "\\v",)
+    def execute(self, name, args, ostream, stack):
+        self.checkArgCount(name, args, exact=[0,])
+        return ttypes.String("\v")
+
 
 
 

File templ/test.templ

-{v
-    {set :SIZE 10}
-}const double sine_lut[{get :SIZE}] =
-\{
-{for i {range {get :SIZE}} {::
-    {let :THETA}
-    {$ :THETA {mult
-        {$ i}
-        {div 360 {$ :SIZE}}
-    }}
-    {spit {'
-        "    "
-        {round 
-            {sin {rad {$ :THETA}}}
-            4
-        }
-        ,
-        {\t}
-        "// i = "
-        {get i}
-        ", theta = "
-        {$ :THETA}
-        " deg"
-        {lnbrk}
-    }}
-}}\};
-
+{echo foo{bar }

File templ/tutorial.css

-pre {
-    border: 1px dashed;
+pre
+{
+    background: none repeat scroll 0 0 #F7F7F7;
+    border: 1px solid #DDDDDD;
+    font-size: 12px;
+    padding: 9px 12px;
+    font-family: Monaco,"Courier New",monospace;
 }

File templ/tutorial.templ

                 "hello, {echo templ}!"
             }
 
-            {p
+            {section "Get Used to Curly Braces!" {'
+                {p
 <<<Ok, still pretty boring, but at least now we're actually writing {templ} code. There's a couple important things to notice in
 this example. For one thing, the format for invoking a function may not be what you're used to in most other languages. {templ}
 uses a syntax known as "S-expressions", similar to Lisp, in which the function to be invoked is the first element in a list whic
 also contains the arguments to be passed to the function. The syntax is semantically the same as the more familiar "M-expressions"
 in which the function is outside of the list of arguments, but the different way of writing it is important for allowing template
 files to be less structured than what you would normally see in source code.>>>
-            }
+                }
+            }}
 
-            {p
-<<<The other thing to notice from this example is that the string we passed to the {code echo} function isn't quoted in any way.>>>
-            }
+            {section "Basic Data Types - Strings and Lists." {'
+                {p
+<<<The other thing to notice from this example is that the string we passed to the {code echo} function isn't quoted in any way.
+{templ} has relatively few data types: for the most part you'll be using primarily {em Strings} and {em Lists}. That means you don't
+need anything special like quotes to differentiate a String: just write it out as part of an expression.>>>
+                }
+
+                {p
+<<<You've already seen a List: the expression {code "{echo templ}"} is a List. You write a List by simply wrapping a sequence of
+elements in curly braces. The elements can be Strings or nested Lists, and are separated by whitespace.>>>
+                }
+                
+                {section "Quoted Strings" {'
+                    {p
+<<<This poses a problem if you want to include whitespace in one of those Strings, so {templ} allows you to wrap your strings in
+double-quote characters, just like you're probably used to in other programming languages. Quoted Strings are only terminated by
+a closing double-quote character, so they can include whitespace (even vertical whitespace, like linebreaks). You also need to use
+a quoted String if you want to include a curly brace in a String, otherwise {templ} will think it's the beginning (or end) or a
+List.>>>
+                    }
+
+                    {p
+<<<If you want to include a double-quote character in a quoted String, you need to escape it with a leading backslash character: {wrap "\"" \}
+Without the backslash, the double-quote will just tell {templ} to terminate the quoting and will not be included in the String.
+Similarly, if you want to include a backslash in a quoted-string, you need to escape it with another leading backslash.>>>
+                    }
+
+                    {p
+<<<The Backslash and double-quote characters are the {em only} characters that have a special meaning when escaped in a quoted String.
+If any other character in a quoted String is prefixed with a backslash, the backslash is simply ignored, and the following character
+is treated normally. Put another way, a backslash character in a quoted String simply tells {templ} to include the subsequent
+character in the String, and ignore any special syntactic meaning it would otherwise have (which only double-quote and backslash
+characters have, anyway).>>>
+                    }
+
+                    {p
+<<<To be perfectly clear, {templ} quoted Strings {em do not have} the special escape sequences you might be used to from other programming
+languages, such as "\\n" for linebreaks, "\\t" for horizontal tab, etc. Since these characters have no special meaning in {templ} quoted Strings,
+you can just include them directly in your quoted String. Alternatively, there are built-in macros such as {code "{\\n}"} and
+{code "{\\t}"} for all of the common escape sequences (specifically, those defined by the ISO C99 standard), as well as the built-in {code chr} function
+which returns a one-character String with character-value equal to a specified numeric value.>>>
+                    }
+
+                    {EXAMPLE-TEMPLATE "text"
+                        {implode
+<<<A final note about quoted Strings: a quoted String is not necessarily an isolated value, it can also be a portion of another String.
+In other words, you can tie together quoted and non-quoted Strings to your heart's content, as long as there's no spaces or curly braces
+in between them. For example:>>>
+                        }
+
+                        {implode
+<<<Lexically, there are four different elements here: the two quoted Strings {code "\"he\""} and {code "\" tem\""}, and
+the two {em unquoted} Strings {code "\"llo,\""} and {code "\"ple!\""}.
+Stringing together all of these different lexical elements results in just a single String value: the output is shown here:>>>
+                        }
+
+                        "{echo \"he\"llo,\" tem\"ple!}"
+
+                    }
+
+                }} % end section - 
+            }} % end section - "Basic Data Types"
 
 
         }} % end section "Hello, ..."