サーバーサイドのDOMと構文解析

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

サーバーサイドのDOMと構文解析

elements

DIVヘルパーとそのすべての派生ヘルパーは、検索メソッドのelementとelementsを提供します。

The DIV helper and all derived helpers provide the search methods element and elements.

elementは、指定した条件にマッチする最初の子要素を返します(マッチするものがない場合、Noneを返します)。

element returns the first child element matching a specified condition (or None if no match).

elementsは、すべてのマッチした子のリストを返します。

elements returns a list of all matching children.

elementとelementsはマッチ条件を指定するのに同じ構文を用います。それは、混合して照合することのできる3種類の選択肢があります:jQuery風の表現と、正確に属性値と照合するものと、正規表現を使用して照合するものです。

element and elements use the same syntax to specify the matching condition which allows for three possibilities that can be mixed and matched: jQuery-like expressions, match by exact attribute value, match using regular expressions.

ここでは簡単な例を示します:

Here is a simple example:

1.

2.

3.

4.

5.

>>> a = DIV(DIV(DIV('a', _id='target',_class='abc')))

>>> d = a.elements('div#target')

>>> d[0][0] = 'changed'

>>> print a

<div><div><div id="target" class="abc">changed</div></div></div>

要素の無名引数は、次のものを含む文字列です:タグの名前、ポンド記号が前に付くタグのID、ドットが前に付くクラス、角括弧内にある属性の明示的な値です。

The un-named argument of elements is a string which may contain: the name of a tag, the id of a tag preceded by a pound symbol, the class preceded by a dot, the explicit value of an attribute in square brackets.

ここでは、前述のタグをIDで検索するための4つの同等な方法を示します:

Here are 4 equivalent ways to search the previous tag by id:

1.

2.

3.

4.

>>> d = a.elements('#target')

>>> d = a.elements('div#target')

>>> d = a.elements('div[id=target]')

>>> d = a.elements('div',_id='target')

ここでは、前述のタグをクラスで検索するための4つの同等な方法を示します:

Here are 4 equivalent ways to search the previous tag by class:

1.

2.

3.

4.

>>> d = a.elements('.abc')

>>> d = a.elements('div.abc')

>>> d = a.elements('div[class=abc]')

>>> d = a.elements('div',_class='abc')

任意の属性は、要素の場所を特定するのに用いることができます(idやclassだけではありません)。複数の属性(element関数は複数の名前つき引数を取ることができます)も用いることができますが、最初にマッチした要素だけが返されます。

Any attribute can be used to locate an element (not just id and class), including multiple attributes (the function element can take multiple named arguments) but only the first matching element will be returned.

jQueryの構文"div#target"を用いて、スペースで区切られた複数の検索条件を指定することも可能です:

Using the jQuery syntax "div#target" it is possible to specify multiple search criteria separated by a space:

1.

2.

>>> a = DIV(SPAN('a', _id='t1'),DIV('b',_class='c2'))

>>> d = a.elements('span#t1, div#c2')

または次のように書けます

or equivalently

1.

2.

>>> a = DIV(SPAN('a', _id='t1'),DIV('b',_class='c2'))

>>> d = a.elements('span#t1','div#c2')

検索属性の値が名前つき引数で指定されている場合、それは文字列か正規表現を取ることができます:

If the value of search attribute is specified using a name argument, it can be a string or a regular expression:

1.

2.

>>> a = DIV(SPAN('a', _id='test123'),DIV('b',_class='c2'))

>>> d = a.elements('span',_id=re.compile('test\d{3}')

DIV(とその派生)ヘルパーの特別な名前つき引数はfindです。これは、タグのテキストの内容における、検索の値、または、検索の正規表現を指定するために用いられます。たとえば次のようになります:

A special named argument of the DIV (and derived) helpers is find. It can be used to specify a search value or a search regular expression in the text content of the tag. For example:

1.

2.

3.

4.

>>> a = DIV(SPAN('abcde'),DIV('fghij'))

>>> d = a.elements(find='bcd')

>>> print d[0]

<span>abcde</span>

または

or

1.

2.

3.

4.

>>> a = DIV(SPAN('abcde'),DIV('fghij'))

>>> d = a.elements(find=re.compile('fg\w{3}'))

>>> print d[0]

<div>fghij</div>

components

html文字列のすべての要素を列挙する例を示します:

Here's an example of listing all elements in an html string:

1.

2.

html = TAG('<a>xxx</a><b>yyy</b>')

for item in html.components: print item

parent

parentは、現在の要素の親を返します。

parent returns the parent of the current element.

1.

2.

3.

4.

5.

>>> a = DIV(SPAN('a'),DIV('b'))

>>> d = a.element('a').parent()

>>> d['_class']='abc'

>>> print a

<div class="abc"><span>a</span><div>b</div></div>

flatten

flattenメソッドは、再帰的に、与えられた要素の子要素の中身を標準の(タグなし)テキストにシリアライズします:

The flatten method recursive serializes the content of the children of a given element into regular text (without tags):

1.

2.

3.

>>> a = DIV(SPAN('this',DIV('is',B('a'))),SPAN('test'))

>>> print a.flatten()

thisisatest

flattenには、オプション引数のrenderを渡すことができます。それは、異なるプロトコルを使用して中身をレンダリング/フラット化する関数です。次に示すのは、いくつかのタグをMarkminのwiki構文へとシリアライズする例です:

Flatten can be passed an optional argument, render, i.e. a function that renders/flattens the content using a different protocol. Here is an example to serialize some tags into Markmin wiki syntax:

1.

2.

3.

4.

5.

6.

>>> a = DIV(H1('title'),P('example of a ',A('link',_href='#test')))

>>> from gluon.html import markmin_serializer

>>> print a.flatten(render=markmin_serializer)

# titles

example of [[a link #test]]

書き込む際には、markmin_serializerとmarkdown_serializerが用意されています。

At the time of writing we provide markmin_serializer and markdown_serializer.

構文解析

Parsing

TAGオブジェクトはXML/HTMLの構文解析器でもあります。テキストを読み込んで、ヘルパーのツリー構造へと変換することができます。これにより、上記のAPIを利用した操作を可能にします:

The TAG object is also an XML/HTML parser. It can read text and convert into a tree structure of helpers. This allows manipulation using the API above:

1.

2.

3.

4.

5.

>>> html = '<h1>Title</h1><p>this is a <span>test</span></p>'

>>> parsed_html = TAG(html)

>>> parsed_html.element('span')[0]='TEST'

>>> print parsed_html

<h1>Title</h1><p>this is a <span>TEST</span></p>