Thread: [Docstring-checkins] CVS: dps/dps utils.py,1.18,1.19
Status: Pre-Alpha
Brought to you by:
goodger
From: David G. <go...@us...> - 2002-03-16 05:57:34
|
Update of /cvsroot/docstring/dps/dps In directory usw-pr-cvs1:/tmp/cvs-serv23805/dps/dps Modified Files: utils.py Log Message: - Added name -> id conversion, ``id()``. - Updated extension attribute support. Index: utils.py =================================================================== RCS file: /cvsroot/docstring/dps/dps/utils.py,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** utils.py 13 Mar 2002 02:43:47 -0000 1.18 --- utils.py 16 Mar 2002 05:57:30 -0000 1.19 *************** *** 11,15 **** """ ! import sys import nodes --- 11,15 ---- """ ! import sys, re import nodes *************** *** 184,188 **** class ExtensionAttributeError(Exception): pass ! class BadAttributeLineError(ExtensionAttributeError): pass class BadAttributeDataError(ExtensionAttributeError): pass class DuplicateAttributeError(ExtensionAttributeError): pass --- 184,188 ---- class ExtensionAttributeError(Exception): pass ! class BadAttributeError(ExtensionAttributeError): pass class BadAttributeDataError(ExtensionAttributeError): pass class DuplicateAttributeError(ExtensionAttributeError): pass *************** *** 204,208 **** function). - `DuplicateAttributeError` for duplicate attributes. ! - `BadAttributeError` for input lines not enclosed in brackets. - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). --- 204,208 ---- function). - `DuplicateAttributeError` for duplicate attributes. ! - `BadAttributeError` for invalid fields. - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). *************** *** 221,225 **** :Exceptions: ! - `BadAttributeError` for input lines not enclosed in brackets.? - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). --- 221,225 ---- :Exceptions: ! - `BadAttributeError` for invalid fields. - `BadAttributeDataError` for invalid attribute data (missing name, missing data, bad quotes, etc.). *************** *** 232,241 **** name = field[0].astext().lower() body = field[1] ! if len(body) != 1 or not isinstance(body[0], nodes.paragraph) \ or len(body[0]) != 1 or not isinstance(body[0][0], nodes.Text): raise BadAttributeDataError( ! 'extension attribute field body may consist of\n' 'a single paragraph only (attribute "%s")' % name) ! data = body[0][0].astext() attlist.append((name, data)) return attlist --- 232,244 ---- name = field[0].astext().lower() body = field[1] ! if len(body) == 0: ! data = None ! elif len(body) > 1 or not isinstance(body[0], nodes.paragraph) \ or len(body[0]) != 1 or not isinstance(body[0][0], nodes.Text): raise BadAttributeDataError( ! 'extension attribute field body may contain\n' 'a single paragraph only (attribute "%s")' % name) ! else: ! data = body[0][0].astext() attlist.append((name, data)) return attlist *************** *** 264,269 **** try: attributes[name] = convertor(value) ! except ValueError, detail: ! raise ValueError('(attribute "%s") %s' % (name, detail)) return attributes --- 267,273 ---- try: attributes[name] = convertor(value) ! except (ValueError, TypeError), detail: ! raise detail.__class__('(attribute "%s", value "%r") %s' ! % (name, value, detail)) return attributes *************** *** 320,323 **** --- 324,369 ---- """Return a case- and whitespace-normalized name.""" return ' '.join(name.lower().split()) + + def id(string): + """ + Convert `string` into an identifier and return it. + + Docutils identifiers will conform to the regular expression + ``[a-z][-a-z0-9]*``. For CSS compatibility, identifiers (the "class" and + "id" attributes) should have no underscores, colons, or periods. Hyphens + may be used. + + - The `HTML 4.01 spec`_ defines identifiers based on SGML tokens: + + ID and NAME tokens must begin with a letter ([A-Za-z]) and may be + followed by any number of letters, digits ([0-9]), hyphens ("-"), + underscores ("_"), colons (":"), and periods ("."). + + - However the `CSS1 spec`_ defines identifiers based on the "name" token, + a tighter interpretation ("flex" tokenizer notation; "latin1" and + "escape" 8-bit characters have been replaced with entities):: + + unicode \\[0-9a-f]{1,4} + latin1 [¡-ÿ] + escape {unicode}|\\[ -~¡-ÿ] + nmchar [-a-z0-9]|{latin1}|{escape} + name {nmchar}+ + + The CSS1 "nmchar" rule does not include underscores ("_"), colons (":"), + or periods ("."), therefore "class" and "id" attributes should not contain + these characters. They should be replaced with hyphens ("-"). Combined + with HTML's requirements (the first character must be a letter; no + "unicode", "latin1", or "escape" characters), this results in the + ``[a-z][-a-z0-9]*`` pattern. + + .. _HTML 4.01 spec: http://www.w3.org/TR/html401 + .. _CSS1 spec: http://www.w3.org/TR/REC-CSS1 + """ + id = non_id_chars.sub('-', normname(string)) + id = non_id_at_ends.sub('', id) + return str(id) + + non_id_chars = re.compile('[^a-z0-9]+') + non_id_at_ends = re.compile('^[-0-9]+|-+$') def newdocument(languagecode='en', warninglevel=2, errorlevel=4, |