Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'LAML
27.  HTML mirror functions in LAML

In this chapter we will elaborate on the HTML mirror functions, which we have encountered numerous times during the initial parts of this material.

The mirror functions are generated automatically from a document type definition of HTML.

The apparatus for generation of HTML mirrors is also available for XML languages. This is XML-in-LAML. We have used XML-in-LAML for several non-trivial tasks. The source of this material is authored in an XML-in-LAML language called LENO.

27.1 The HTML mirrors in LAML27.4 Mirroring of HTML (3)
27.2 Mirroring of HTML (1)27.5 Mirroring of HTML (4)
27.3 Mirroring of HTML (2)27.6 Summary of Mirror Rules
 

27.1.  The HTML mirrors in LAML
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

A HTML mirror in Scheme is a set of functions that in an accurate way makes the HTML elements of a particular HTML version available in Scheme

The key properties of a HTML mirror in Scheme - as provided by LAML - are the following.

  • A one-to-one mapping of HTML elements to named functions in Scheme

  • Generates well-formed and valid HTML documents

  • Prevents accidental emission of '<', '>', and '&' as part of the textual contents

  • The mirror functions return abstract syntax trees which can be rendered as 'HTML text'

  • Supports optional pretty printing of the resulting HTML code

The generation of valid HTML in reality boils down to check of the context free composition of the document on the fly, while it is generated by the mirror functions.

As of the fall of 2003, the generation of the validation procedures is fully automatic. XML elements with so-called element contents [xml10] (as opposed to the elements with mixed content, i.e. elements with alternatives and PCDATA) are validated by deterministic finite state automata.

The point below is rather naive, but nevertheless important.

Not too many functions - not too few. You cannot by accident use a non-standard HTML element

Using raw XML you can by accident use a non-existing tag or element. When used through LAML you will discover this as soon as you attempt to execute the LAML document - simply because the counterpart of the non-existing tag is a non-defined Scheme function.

In Section 27.2 and the subsequent sections we will describe and discuss the detailed rules of the mirror functions. The rules will be summarized in Section 27.6.

 

27.2.  Mirroring of HTML (1)
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

We describe the mirror functions in this and the following sections.

We here focus on the basic elements and their composition. As a contrast to Program 26.2 from the previous chapter, we also include the attributes in the forms below

The mirror function f distinguishes between attribute names, attribute values, explicit white space,character references,and content strings via the runtime types of the parameters together with their mutual position in the parameter list.

1
(f 'a1 "v1" 'a2 "v2" "Some text." "More text")
Program 27.1    A HTML mirror expression with attribute names (symbols), attribute values (strings following symbols) and content strings.

The LAML Scheme form in Program 27.1 corresponds to the HTML form in Program 27.2.

1
<f a1="v1" a2="v2"> Some text. More text</f>
Program 27.2    The rendering of the value of the HTML mirror expression.

Attribute names are represented as symbols. The string that follows a symbol is the attribute value. All other strings are 'white space concatenated' to the element instance contents. The details of the white space handling is described in Section 27.3.

As a consequence, and as a generalization in relation to HTML and XML, the attributes and the contents may be mixed arbitrarily. As an example, all the attributes may come after the content strings.

The HTML mirror functions traverse the actual parameters and 'sorts' them into attributes and contents contributions. From a programming language point of view, the attributes are handled as keyword parameters - although simulated with use of symbols as explained above.

 

27.3.  Mirroring of HTML (2)
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

The handling of white spaces is a minor detail. Nevertheless, it is a detail which is important to 'get right'.

The basic idea behind the handling of white spaces in LAML is formulated in the following point.

Instead of specifying where to add white spaces we tell where to suppress it

The underscore symbols (shown with red emphasis) in Program 27.3 suppress white space. Underlying, the underscore symbol is bound to a boolean false value. This is rather arbitrary. What matters is really that a distinguished and unique run-time value is used for the purpose. Symbols and strings are ruled out, because they are used already for content and attributes. A boolean value is fine.

1
2
(p "Use" (kbd "HTML") _ ","  (kbd "XHTML") _ ","
        (kbd "XML")_ "," "or" (kbd "LAML") _ ".")
Program 27.3    An HTML mirror expression which suppresses white space in front of punctuation characters.

The Scheme fragment in Program 27.3 returns an internal structure, which can be rendered as shown in Program 27.4.

1
2
3
<p>
  Use <kbd>HTML</kbd>, <kbd>XHTML</kbd>, <kbd>XML</kbd>, or <kbd>LAML</kbd>.
</p>
Program 27.4    The rendering of the value of the HTML mirror expression.

 

27.4.  Mirroring of HTML (3)
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

Until now we have met symbols, strings and boolean values as parameters in the HTML mirror functions. We have not yet used lists. We will do that now.

When a list is encountered among the parameters of a HTML mirror function, the elements of the list are spliced into the surrounding parameter lists. The implementation of the splicing is recursive, such that lists of any depth are flattened and spliced in the contextual list of HTML mirror function parameters.

It turns out that this handling of this is extremely flexible and important in a language, where the primary structuring of data is done with lists.

List of contents and lists of attributes are processed recursively and spliced together with their context

In Program 27.5 we see an ul unordered list, in which the contents is passed as a list. The list is formed by mapping the li mirror function on (list( "one" "two" "three").

1
2
3
4
5
6
7
8
9
10
11
  (body

    (ul 
      (map li (list "one" "two" "three"))
    )

    
    (let ((attributes
           (list 'start "3" 'css:list-style-type "lower-roman"))
          (contents (map li (list "one" "two" "three"))))
       (ol 'id "demo" contents attributes)))
Program 27.5    An HTML mirror expression in which lists are passed as parameters to the HTML mirror functions.

With the convention of splicing lists into its surround, the example would be more clumsy too, because we then have to apply ul on a certain list, using apply, cf. Section 25.4.

In the let construct of Program 27.5 we bind the blue names attributes and contents to two lists. Both lists are spliced into the list with the id symbol and the "demo" string. In the ol form we just refer to these lists via their names. Notice that the id attribute of ol is passed 'the normal way'.

1
2
3
4
5
6
7
8
9
<body>
    <ul><li>one</li> <li>two</li> <li>three</li></ul>
       
    <ol style="list-style-type: lower-roman;" id="demo" start="3">
        <li>one</li>
        <li>two</li>
        <li>three</li>
    </ol>
</body>
Program 27.6    The rendering of the value of the HTML mirror expression.

 

27.5.  Mirroring of HTML (4)
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In this section we show how CSS attributes - Cascading Style Sheet attributes - are handled in the HTML mirror functions.

CSS attributes and HTML attributes are uniformly specified

CSS attributes are prefixed with css:

1
(em 'css:background-color "yellow" "Functional Programming in Scheme")
Program 27.7    An HTML mirror form in which we highlight a CSS attribute.

The main point to notice is the uniform notation used for both HTML attributes and CSS attributes. The notation is somehow in conflict with the notation used for name spaces in XML.

The rendering of the expression in Program 27.7 is shown in Program 27.8.

1
<em style="background-color: yellow;">Functional Programming in Scheme</em>
Program 27.8    The rendering of the value of the HTML mirror expression.

HTML attributes are validated in LAML, but CSS attributes are not.

 

27.6.  Summary of Mirror Rules
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

The parameter passing rules of the HTML mirror functions are summarized below. Notice that the exact same rules apply for XML mirror functions in the so-called XML-in-LAML framework.

The HTML mirror conventions of LAML can be summarized in six rules

  • Rule 1
    An attribute name is a symbol in Scheme, which must be followed by an expression of type string, which plays the role as the attribute's value.

  • Rule 2
    Parameters which do not follow a symbol are content elements (strings or instances of elements).

  • Rule 3
    All content elements are implicitly separated by white space.

  • Rule 4
    A distinguished data object (the boolean value false) which we conveniently bind to a variable named _ suppresses white space at the location where the value appears.

  • Rule 5
    Every place an attribute or a content element is accepted we also accept a list, the elements of which are processed recursively and unfolded into the result.

  • Rule 6
    An attribute with the name css: a refers to the a attribute in CSS.

In the next chapter we will discuss a number of practical aspects of the LAML system.

 

27.7.  References
[Xml10]World Wide Web Consortium, "Extensible Markup Language (XML) 1.0", February 1998.

Generated: Tuesday July 2, 2013, 09:16:05
Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'