2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
6 Directives for typically HTML-specific constructs.
9 __docformat__
= 'reStructuredText'
12 from docutils
import nodes
, utils
13 from docutils
.parsers
.rst
import Directive
14 from docutils
.parsers
.rst
import states
15 from docutils
.transforms
import components
18 class MetaBody(states
.SpecializedBody
):
20 class meta(nodes
.Special
, nodes
.PreBibliographic
, nodes
.Element
):
21 """HTML-specific "meta" element."""
24 def field_marker(self
, match
, context
, next_state
):
26 node
, blank_finish
= self
.parsemeta(match
)
28 return [], next_state
, []
30 def parsemeta(self
, match
):
31 name
= self
.parse_field_marker(match
)
32 indented
, indent
, line_offset
, blank_finish
= \
33 self
.state_machine
.get_first_known_indented(match
.end())
35 pending
= nodes
.pending(components
.Filter
,
36 {'component': 'writer',
39 node
['content'] = ' '.join(indented
)
41 line
= self
.state_machine
.line
42 msg
= self
.reporter
.info(
43 'No content for meta tag "%s".' % name
,
44 nodes
.literal_block(line
, line
))
45 return msg
, blank_finish
48 attname
, val
= utils
.extract_name_value(tokens
[0])[0]
49 node
[attname
.lower()] = val
50 except utils
.NameValueError
:
51 node
['name'] = tokens
[0]
52 for token
in tokens
[1:]:
54 attname
, val
= utils
.extract_name_value(token
)[0]
55 node
[attname
.lower()] = val
56 except utils
.NameValueError
, detail
:
57 line
= self
.state_machine
.line
58 msg
= self
.reporter
.error(
59 'Error parsing meta tag attribute "%s": %s.'
60 % (token
, detail
), nodes
.literal_block(line
, line
))
61 return msg
, blank_finish
62 self
.document
.note_pending(pending
)
63 return pending
, blank_finish
66 class Meta(Directive
):
70 SMkwargs
= {'state_classes': (MetaBody
,)}
73 self
.assert_has_content()
74 node
= nodes
.Element()
75 new_line_offset
, blank_finish
= self
.state
.nested_list_parse(
76 self
.content
, self
.content_offset
, node
,
77 initial_state
='MetaBody', blank_finish
=True,
78 state_machine_kwargs
=self
.SMkwargs
)
79 if (new_line_offset
- self
.content_offset
) != len(self
.content
):
80 # incomplete parse of block?
81 error
= self
.state_machine
.reporter
.error(
82 'Invalid meta directive.',
83 nodes
.literal_block(self
.block_text
, self
.block_text
),