parsing-brainstorming: Difference between revisions
(→Examples: Datetime and some combined examples) |
(→General Algorithm: Expand to deal with embedded microformats. This is probably wrong. I'll proof read tomorrow.) |
||
| Line 5: | Line 5: | ||
= General Algorithm = | = General Algorithm = | ||
# Make a ''copy'' of the DOM tree | # Make a ''copy'' of the DOM tree to operate on. All future references to DOM traversal and manipulation refer to this clone. | ||
# Implement [[include-pattern|the include pattern]] by removing any nodes with <code>class="include"</code> and replacing them with the node which they point to. | # Implement [[include-pattern|the include pattern]] by removing any nodes with <code>class="include"</code> and replacing them with the node which they point to. | ||
# Parse each property | # Parse all properties that may contain nested microformats. For example within hCard, the agent property may contain a nested hCard. For each property <code>prop</code> which may contain a nested microformat: | ||
## Create an empty array to store the value(s) of <code>prop</code> in. Call this <code>A</code>. | |||
There are three different categories of property — singular, plural and concatenated. Most properties are either singular or plural, but a handful are concatenated, such as <code>entry-summary</code> in [[hAtom]]. | ## OUTER: Find all elements with <code>class="prop"</code> that are descended from <code>root</code>. For each of those elements <code>e</code>: | ||
### INNER: For each microformat root class name that might be present (e.g. within an hCalendar event's <code>location</code> property you might reasonably expect to find an hCard, and adr or a geo!): | |||
# Create an empty array to store the value(s) of <code>prop</code> in. Call this <code>A</code>. | #### Create an empty list <code>F</code>. | ||
# Find all elements with <code>class="prop"</code> that are descended from <code>root</code> | #### Search for any elements within <code>e</code> which have the microformat root class name. Bear in mind that perhaps this element might ''be'' <code>e</code> — this is allowed. For each such element, <code>e2</code>: | ||
# For each element <code>e</code>, run this: | ##### Parse <code>e2</code> as an embedded microformat and place the result in <code>F</code> if it is valid. | ||
## Find the value of <code>e</code>, using the techniques in the section below. | ##### Run the DESTROY_ELEMENT function on <code>e2</code> | ||
## If the value of <code>e</code> is not NULL, add it to <code>A</code> | #### If <code>F</code> is not empty: | ||
## If the <code>prop</code> is a singular property and <code>A</code> is not empty, jump out of this foreach loop. | ##### Move the first item in <code>F</code> to <code>A</code> | ||
# If <code>prop</code> is a singular property, then its value is <code>A[0]</code>. | ##### Jump out of the INNER loop. | ||
# If <code>prop</code> is a plural property, then its values are <code>A</code>. | ### If <code>A</code> is not empty: | ||
# If <code>prop</code> is a concatenated property, then its values are formed by concatenating the values of <code>A</code> together. | #### Change the <code>class</code> attribute on <code>e</code>, removing <code>prop</code> from the class list. | ||
#### If property <code>prop</code> is singular, jump out of the OUTER loop. | |||
## If <code>prop</code> is a singular property, let <code>A[0]</code> be its value. If plural, let <code>A</code> be the list of its values. | |||
# Run the DESTROYER function on <code>root</code>. | |||
# Parse all properties. There are three different categories of property — singular, plural and concatenated. Most properties are either singular or plural, but a handful are concatenated, such as <code>entry-summary</code> in [[hAtom]]. For each property <code>prop</code> within <code>root</code>: | |||
## Create an empty array to store the value(s) of <code>prop</code> in. Call this <code>A</code>. | |||
## Find all elements with <code>class="prop"</code> that are descended from <code>root</code>. | |||
## For each element <code>e</code>, run this: | |||
### Find the value of <code>e</code>, using the techniques in the section below. | |||
### If the value of <code>e</code> is not NULL, add it to <code>A</code> | |||
### If the <code>prop</code> is a singular property and <code>A</code> is not empty, jump out of this foreach loop. | |||
## If <code>prop</code> is a singular property, then its value is <code>A[0]</code>. | |||
## If <code>prop</code> is a plural property, then its values are <code>A</code>. | |||
## If <code>prop</code> is a concatenated property, then its values are formed by concatenating the values of <code>A</code> together. | |||
= Finding Values = | = Finding Values = | ||
Revision as of 18:32, 23 July 2008
This is an attempt to get some of my thoughts on parsing, from practical experience implementing Cognition, out of my head and onto the wiki. Hopefully it will replace parsing once it reaches consensus, as this document is somewhat more detailed. It deals with how to parse the properties of a compound microformat once we have located the root element, which we shall call root. It only deals with simple properties which have no sub-properties, but 90% of properties do fall into this category. (And many of the others can be parsed by treating the property element as root and then finding sub-properties using the techniques on this page.) TobyInk
Note: as a courtesy, I'd like to ask people not to edit this page for the next few days, until I have gotten the initial version stable. Thanks. TobyInk 01:14, 21 Jul 2008 (PDT)
General Algorithm
- Make a copy of the DOM tree to operate on. All future references to DOM traversal and manipulation refer to this clone.
- Implement the include pattern by removing any nodes with
class="include"and replacing them with the node which they point to. - Parse all properties that may contain nested microformats. For example within hCard, the agent property may contain a nested hCard. For each property
propwhich may contain a nested microformat:- Create an empty array to store the value(s) of
propin. Call thisA. - OUTER: Find all elements with
class="prop"that are descended fromroot. For each of those elementse:- INNER: For each microformat root class name that might be present (e.g. within an hCalendar event's
locationproperty you might reasonably expect to find an hCard, and adr or a geo!):- Create an empty list
F. - Search for any elements within
ewhich have the microformat root class name. Bear in mind that perhaps this element might bee— this is allowed. For each such element,e2:- Parse
e2as an embedded microformat and place the result inFif it is valid. - Run the DESTROY_ELEMENT function on
e2
- Parse
- If
Fis not empty:- Move the first item in
FtoA - Jump out of the INNER loop.
- Move the first item in
- Create an empty list
- If
Ais not empty:- Change the
classattribute one, removingpropfrom the class list. - If property
propis singular, jump out of the OUTER loop.
- Change the
- INNER: For each microformat root class name that might be present (e.g. within an hCalendar event's
- If
propis a singular property, letA[0]be its value. If plural, letAbe the list of its values.
- Create an empty array to store the value(s) of
- Run the DESTROYER function on
root. - Parse all properties. There are three different categories of property — singular, plural and concatenated. Most properties are either singular or plural, but a handful are concatenated, such as
entry-summaryin hAtom. For each propertypropwithinroot:- Create an empty array to store the value(s) of
propin. Call thisA. - Find all elements with
class="prop"that are descended fromroot. - For each element
e, run this:- Find the value of
e, using the techniques in the section below. - If the value of
eis not NULL, add it toA - If the
propis a singular property andAis not empty, jump out of this foreach loop.
- Find the value of
- If
propis a singular property, then its value isA[0]. - If
propis a plural property, then its values areA. - If
propis a concatenated property, then its values are formed by concatenating the values ofAtogether.
- Create an empty array to store the value(s) of
Finding Values
There are at least five different types of property that can be parsed, each of which requires different techniques:
- HTML properties, such as
entry-contentin hAtom - URI properties, such as
urlin hCard - ID properties, such as
uidin hCard - Datetime properties, such as
dtstartin hCalendar - Plain text properties, such as
titlein hCard
Arguments can be made for duration properties and numeric properties to also have variations in the algorithm, but for now, we'll just treat them as plain text properties.
HTML Properties
These are the easiest to parse. Given an element e, just use the HTML representation of its DOM node. Some DOM implementations make this available as .outerHTML.
URI Properties
Certain HTML elements are capable of linking to other resources. The most obvious is <a> though there are many others. The following list of linking elements is derived from Perl's HTML::Tagset module:
{
'a' => ['href'],
'applet' => ['codebase', 'archive', 'code'],
'area' => ['href'],
# 'base' => ['href'],
'bgsound' => ['src'],
'blockquote' => ['cite'],
# 'body' => ['background'],
'del' => ['cite'],
'embed' => ['src', 'pluginspage'],
'form' => ['action'],
'frame' => ['src', 'longdesc'],
'iframe' => ['src', 'longdesc'],
# 'ilayer' => ['background'],
'img' => ['src', 'lowsrc', 'longdesc', 'usemap'],
'input' => ['src', 'usemap'],
'ins' => ['cite'],
'isindex' => ['action'],
# 'head' => ['profile'],
'layer' => ['src'], # 'background'
'link' => ['href'],
'object' => ['data', 'classid', 'codebase', 'archive', 'usemap'],
'q' => ['cite'],
'script' => ['src', 'for'],
# 'table' => ['background'],
# 'td' => ['background'],
# 'th' => ['background'],
# 'tr' => ['background'],
'xmp' => ['href'],
}
Note that some are commented out as they might be too counter-intuitive to implement!
If we're parsing an element e and looking for a URI, here is the algorithm we use:
- Set variable
uto NULL. - Search
efor any descendent elements withclass="value". Call this listV. - Add the element
eitself to the listV, at the front of the list. - OUTER: for each element
vfrom listV:- If
vis a linking element from the above list- INNER: for each attribute
aassociated that the tag name ofvin the above list- If
ais set- Set
uto the contents ofa - Jump out of the OUTER loop.
- Set
- If
- INNER: for each attribute
- If
- If
uis not null, and is a relative URI, convert it to an absolute URI.
The URI has hopefully been found in u. If no URI has been found, then fall back to plain text parsing.
UID Properties
UID properties are parsed similarly to URL properties, but with a slightly modified algorithm, allowing for UIDs to be specified in the id attribute. The following example has a UID of "http://example.com/page#foo".
<base href="http://example.com/page" /> <div class="uid" id="foo">...</div>
The modified algorithm used is:
- Set variable
uto NULL. - Search
efor any descendent elements withclass="value". Call this listV. - Add the element
eitself to the listV, at the front of the list. - OUTER: for each element
vfrom listV:- If
vis a linking element from the above list- INNER: for each attribute
aassociated that the tag name ofvin the above list- If
ais set- Set
uto the contents ofa - Jump out of the OUTER loop.
- Set
- If
- INNER: for each attribute
- If
vhas anidattribute set- Set
uto the contents ofid, with the character "#" prepended - Jump out of the OUTER loop.
- Set
- If
- If
uis not null, and is a relative URI, convert it to an absolute URI.
Again, if no u has been found by the algorithm, then fall back to parsing it as a plain text property.
Datetime Properties
Parsing property prop, if class="prop" is found on element e.
- If element
ehas an attributedatetime, then the content of that attribute is the value and the rest of these steps should be skipped. - Create a list
D, which is empty. - Create a list
Vof elements withclass="value". - For each element
vinV:- If
vhas an attributedatetime, then add the content of that attribute toD - Otherwise, run the STRINGIFY function on
vand add the result toD
- If
- If
Dis empty, then run the STRINGIFY function oneand let the result be the value, and skip the rest of these steps. - If
Dcontains only one item, and it looks like an ISO date or ISO datetime, then let that be the value, and skip the rest of these steps. - If
Dcontains two items, and the first looks like an ISO date, and the second like a time, concatenate them, joining with an upper case 'T', let that be the value, and skip the rest of these steps. - If
Dcontains three items, and the first looks like an ISO date, the second like a time, and the last like a timezone (may need normalisation), concatenate them, joining the first two with an upper case 'T' and the last one with no intervening character, let that be the value, and skip the rest of these steps. - Concatenate all the items in
Dand let that be the value.
The final value should be interpreted as liberally as possible with regards to punctuation as an ISO date or ISO datetime.
Normalizing Timezones
Where S is a sign (+ or -) and the letters a, b, c, d are numerals, then:
- Sa → S0a00
- Sab → Sab00
- Sabc → S0abc
- Sa: → S0a00
- Sab: → Sab00
- Sa:b → S0ab0
- S:ab → S00ab
- Sa:bc → S0abc
- Sab:c → Sabc0
- Sab:cd → Sabcd
Plain text Properties
To obtain the value of the property, run STRINGIFY on the property node.
Stringification
The STRINGIFY function performs a text serialisation of an HTML node, with a few adjustments to implement the ABBR pattern. It uses a helper function, _STRINGIFY.
STRINGIFY
- First parameter: element to stringify,
e. - Second parameter: whether to perform value excerpting - default yes.
- Third parameter: whether to perform abbr pattern - default yes.
- If
eis an<abbr>or<acronym>element, and has atitleattribute, then return that attribute. - If you want to implement any proposed alternatives to the ABBR pattern, then here is the place to do so.
- If value excerpting is enabled:
- Create an empty list
S - Search for any descendant elements of
ewithclass="value". Put these into a listV. - For each element
vinV- Recursion: call STRINGIFY on
v, disabling value excerpting but enabling the ABBR pattern. Add the result toS.
- Recursion: call STRINGIFY on
- Concatenate the items in
Sto form a string. If this string is not empty, then return the string.
- Create an empty list
- Run _STRINGIFY on
e, trim excess white space from the result and return it.
_STRINGIFY
This is a somewhat simplified version of the real algorithm that I use. You probably want to refine it by adding better whitespace handling rules (e.g. line breaks after block elements, asterisks for list items, etc).
_STRINGIFY is called with one parameter, the element e to be stringified.
- If
eis text node (not an element), then return it. - If
eis an<img>tag, return thealttext. - If
eis an<input>tag, return the text of thevalueattribute. - If
eis an<br>tag, return a linebreak character. - If
eis an<del>tag, return a zero-length string. - Otherwise, create an empty list
S. - For each direct child node
cofe:- Run _STRINGIFY on
cand add the result toS.
- Run _STRINGIFY on
- Concatenate the items in list
Sand return them.
Examples
URL Examples
The following are all parsed as the same URL.
<a class="url" href="http://example.com/page">Page</a>
<p class="url"> <a href="http://example.com/not-this">Not this</a> <a class="value" href="http://example.com/page">Page</a> </p>
<p class="url"> <a class="value" href="http://example.com/page">Page</a> <a class="value" href="http://example.com/not-this">Not this</a> </p>
<p class="url"> <span class="value">http://example.com/</span> Not this <span class="value">page</span> </p>
<img class="url" alt="foo" src="http://example.com/page"
longdesc="http://example.com/not-this">
<!-- (invalid, but works) --> <img class="url" longdesc="http://example.com/page">
<!-- (strange, but true) --> <p class="url"> <img src="http://example.com/not-this" alt="http://example.com/page"> </p>
Datetime Examples
<time class="dtstart" datetime="2008-07-21">Monday</time>
<p class="dtstart"> <time class="value" datetime="2008-07-21">Monday</time> at <time class="value" datetime="21:30">9:30pm</time>. </p>
<p class="dtstart"> <time class="value" datetime="2008-07-21">Monday</time> at <time class="value" datetime="21:30">9:30pm</time> <time class="value" datetime="+1">(UK)</time>. </p>
<p class="dtstart"> <abbr class="value" title="2008-07-21">Monday</time> at <abbr class="value" title="T21:30">9:30pm</time> <abbr class="value" title="+0100">(UK)</time>. </p>
Combination Examples
<ins class="rev url" datetime="2008-07-21T21:30:00+0100"> I launched my <a href="http://example.com/" class="value">new website</a> today. </ins>
Parsed as:
- rev = 2008-07-21T21:30:00+0100
- url = http://example.com/