Miscellaneous Functions

The markup algebra includes several miscellaneous functions for converting between different value types, for example turning a string into a page and back, accessing the begin and end tags of a piece (See See Miscellaneous Functions). To give a feeling for how these functions are used, we first define a new page containing a heading and 2-by-2 table:

var P = NewPage("<html><body>

<h1>Test Page</h1>

<table>

<tr>

<td align=center>A</td><td>100</td>

</tr>

<tr>

<td align=center>B</td><td>230</td>

</tr>

</table>

</body></html>", "text/html");

 

Note that the second argument to NewPage defines the parser to be used to parse the string into a page. For the definition above, the following WebL expressions evaluate in the following manner:

Markup(P) // Returns "<html><body> ...

// ... </html>" as above.

var H = Elem(P, "h1")[0] // Returns the first H1.

Markup(H) // Returns "<h1>Test Page</h1>."

 

Text(P) // Returns "Test Page A 100 B 230"

// (including white space).

Text(H) // Returns "Test Page".

 

Name(H) // Returns "h1".

 

var T = Elem(P, "td") // Returns all the TD elements.

Markup(T[0]) // Returns

// "<td align="center">A</td>".

Markup(T[1]) // Returns "<td>100</td>".

Text(T[0]) // Returns "A".

Text(T[1]) // Returns "100".

Name(T[0]) // Returns "td".

 

T[0].align // Returns "center".

 

var x = BeginTag(H), y = EndTag(H);

 

Page(H) == P // Returns true.

Page(x) == P // Returns true.

Page(y) == P // Returns true

 

The Pretty function is similar to the Markup function except that it pretty-prints the markup by indenting elements according to their nesting level. This is useful to study the structure of badly formatted HTML and XML pages. Note that pretty-printing a page involves a reformatting of white spaces and new lines, so the resulting string might differ dramatically from the original page source (sometimes enough to break scripts that worked correctly on the "ugly" page)1.

Miscellaneous Functions

Function

Description

BeginTag(q: piece): tag

Returns the begin tag of a piece.

EndTag(q: piece): tag

Returns the end tag of a piece.

ExpandCharEntities(p: page,
s: string): string

Expands the character entities (eg. &lt;, &amp;) in s to their Unicode character equivalents. The DTD of page p is used for the lookups.

ExpandCharEntities(s: string): string

Expands the character entities (eg. &lt;, &amp;) in s to their Unicode character equivalents. The HTML 4.0 DTD is used for the lookups.

Markup(p: page): string

Turns a page object back into a string.

Markup(q: piece): string

Turns a piece object back into a string.

Name(q: piece): string

Returns the name of a piece, or the empty string in the case of q being unnamed.

NewPage(s: string, mimetype: string): page

Parses the string s with the mimetype indicated markup parser and returns a page object.

NewPiece(s: string,
mimetype: string): piece

Equivalent to Content(NewPage(s, mimetype)).

NewPieceSet(s: set): pieceset

Converts a set of pieces into a piece set. Thows an EmptySet exception should s be empty.

NewPieceSet(p: page): pieceset

Returns an empty pieceset associated with with page p.

Page(q: piece): page

Returns the page a piece belongs to.

Page(t: tag): page

Returns the page a tag belongs to.

Pretty(p: page): string

Returns a pretty-printed version of the page.

Pretty(q: piece): string

Returns a pretty-printed version of a piece.

Size(p: pieceset):int

Returns the number of pieces belonging to p.

Text(p: page): string

Returns the text (sans tags) of a page.

Text(q: piece): string

Returns the text (sans tags) of a piece.


1. WebL tries to ensure that the pretty-printed page still renders in the browser in the same manner as the original page by using some limited inbuilt knowledge about markup. For example, HTML preformatted elements (PRE) are not changed.


Up Previous Next