[Docstring-checkins] CVS: dps/dps/transforms frontmatter.py,1.1,1.2
Status: Pre-Alpha
Brought to you by:
goodger
From: David G. <go...@us...> - 2002-01-26 00:00:53
|
Update of /cvsroot/docstring/dps/dps/transforms In directory usw-pr-cvs1:/tmp/cvs-serv5138/dps/dps/transforms Modified Files: frontmatter.py Log Message: minor refactoring Index: frontmatter.py =================================================================== RCS file: /cvsroot/docstring/dps/dps/transforms/frontmatter.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** frontmatter.py 2002/01/16 02:55:11 1.1 --- frontmatter.py 2002/01/26 00:00:50 1.2 *************** *** 106,123 **** def transform(self, doctree): self.setup_transform(doctree) index = doctree.findnonclass((nodes.comment, nodes.system_warning)) ! if self.checkpromotioncandidate(index): candidate = doctree[index] else: ! return doctree.attributes.update(candidate.attributes) doctree[:] = candidate[:1] + doctree[:index] + candidate[1:] # Check for a lone second-level section. index = doctree.findnonclass((nodes.comment, nodes.system_warning, ! nodes.title)) # new title ! if self.checkpromotioncandidate(index): candidate = doctree[index] else: ! return # Create a subtitle element based on the title element: subtitle = nodes.subtitle() --- 106,132 ---- def transform(self, doctree): self.setup_transform(doctree) + if self.promote_document_title(): + self.promote_document_subtitle() + + def promote_document_title(self): + doctree = self.doctree index = doctree.findnonclass((nodes.comment, nodes.system_warning)) ! if self.check_promotion_candidate(index): candidate = doctree[index] else: ! return None doctree.attributes.update(candidate.attributes) doctree[:] = candidate[:1] + doctree[:index] + candidate[1:] + return 1 + + def promote_document_subtitle(self): + doctree = self.doctree # Check for a lone second-level section. index = doctree.findnonclass((nodes.comment, nodes.system_warning, ! nodes.title)) # skip the new title ! if self.check_promotion_candidate(index): candidate = doctree[index] else: ! return None # Create a subtitle element based on the title element: subtitle = nodes.subtitle() *************** *** 126,138 **** # Put the subtitle element into the doctree doctree[:] = doctree[:1] + [subtitle] + doctree[1:index] + candidate[1:] ! return ! def checkpromotioncandidate(self, index): """ Return 1 iff the index'th child of node should be promoted. """ ! if index is None or len(self.doctree) > (index + 1): ! return None ! if not isinstance(self.doctree[index], nodes.section): return None return 1 --- 135,146 ---- # Put the subtitle element into the doctree doctree[:] = doctree[:1] + [subtitle] + doctree[1:index] + candidate[1:] ! return 1 ! def check_promotion_candidate(self, index): """ Return 1 iff the index'th child of node should be promoted. """ ! if index is None or len(self.doctree) > (index + 1) or \ ! not isinstance(self.doctree[index], nodes.section): return None return 1 *************** *** 211,214 **** --- 219,226 ---- def transform(self, doctree): self.setup_transform(doctree) + self.process_docinfo() + + def process_docinfo(self): + doctree = self.doctree index = doctree.findnonclass((nodes.title, nodes.subtitle, nodes.comment, nodes.system_warning)) *************** *** 218,222 **** if isinstance(candidate, nodes.field_list): biblioindex = doctree.findnonclass((nodes.title, nodes.subtitle)) ! nodelist, remainder = self.extractbibliographic(candidate) if remainder: doctree[index] = remainder --- 230,234 ---- if isinstance(candidate, nodes.field_list): biblioindex = doctree.findnonclass((nodes.title, nodes.subtitle)) ! nodelist, remainder = self.extract_bibliographic(candidate) if remainder: doctree[index] = remainder *************** *** 226,230 **** return ! def extractbibliographic(self, field_list): docinfo = nodes.docinfo() remainder = [] --- 238,242 ---- return ! def extract_bibliographic(self, field_list): docinfo = nodes.docinfo() remainder = [] *************** *** 236,250 **** normedname = utils.normname(name) if not (len(field) == 2 and bibliofields.has_key(normedname) ! and self.checkemptybibliofield(field, name)): raise TransformError biblioclass = bibliofields[normedname] if issubclass(biblioclass, nodes.TextElement): ! if not self.checkcompoundbibliofield(field, name): raise TransformError ! self.filterrcskeywords(field[1][0]) docinfo.append(biblioclass('', '', *field[1][0])) else: # multiple body elements possible if issubclass(biblioclass, nodes.authors): ! self.extractauthors(field, name, docinfo) elif issubclass(biblioclass, nodes.abstract): if abstract: --- 248,262 ---- normedname = utils.normname(name) if not (len(field) == 2 and bibliofields.has_key(normedname) ! and self.check_empty_biblio_field(field, name)): raise TransformError biblioclass = bibliofields[normedname] if issubclass(biblioclass, nodes.TextElement): ! if not self.check_compound_biblio_field(field, name): raise TransformError ! self.filter_rcs_keywords(field[1][0]) docinfo.append(biblioclass('', '', *field[1][0])) else: # multiple body elements possible if issubclass(biblioclass, nodes.authors): ! self.extract_authors(field, name, docinfo) elif issubclass(biblioclass, nodes.abstract): if abstract: *************** *** 268,272 **** return [docinfo], field_list ! def checkemptybibliofield(self, field, name): if len(field[1]) < 1: field[-1] += self.doctree.reporter.error( --- 280,284 ---- return [docinfo], field_list ! def check_empty_biblio_field(self, field, name): if len(field[1]) < 1: field[-1] += self.doctree.reporter.error( *************** *** 275,279 **** return 1 ! def checkcompoundbibliofield(self, field, name): if len(field[1]) > 1: field[-1] += self.doctree.reporter.error( --- 287,291 ---- return 1 ! def check_compound_biblio_field(self, field, name): if len(field[1]) > 1: field[-1] += self.doctree.reporter.error( *************** *** 288,292 **** return 1 ! rcskeywordsubstitutions = [ (re.compile(r'\$' r'Date: (\d\d\d\d)/(\d\d)/(\d\d) [\d:]+ \$$', re.IGNORECASE), r'\1-\2-\3'), --- 300,304 ---- return 1 ! rcs_keyword_substitutions = [ (re.compile(r'\$' r'Date: (\d\d\d\d)/(\d\d)/(\d\d) [\d:]+ \$$', re.IGNORECASE), r'\1-\2-\3'), *************** *** 295,302 **** (re.compile(r'\$[a-zA-Z]+: (.+) \$$'), r'\1'),] ! def filterrcskeywords(self, paragraph): if len(paragraph) == 1 and isinstance(paragraph[0], nodes.Text): textnode = paragraph[0] ! for pattern, substitution in self.rcskeywordsubstitutions: match = pattern.match(textnode.data) if match: --- 307,314 ---- (re.compile(r'\$[a-zA-Z]+: (.+) \$$'), r'\1'),] ! def filter_rcs_keywords(self, paragraph): if len(paragraph) == 1 and isinstance(paragraph[0], nodes.Text): textnode = paragraph[0] ! for pattern, substitution in self.rcs_keyword_substitutions: match = pattern.match(textnode.data) if match: *************** *** 304,318 **** return ! def extractauthors(self, field, name, docinfo): try: if len(field[1]) == 1: if isinstance(field[1][0], nodes.paragraph): ! authors = self.authorsfrom1paragraph(field) elif isinstance(field[1][0], nodes.bullet_list): ! authors = self.authorsfrombulletlist(field) else: raise TransformError else: ! authors = self.authorsfromparagraphs(field) authornodes = [nodes.author('', '', *author) for author in authors if author] --- 316,330 ---- return ! def extract_authors(self, field, name, docinfo): try: if len(field[1]) == 1: if isinstance(field[1][0], nodes.paragraph): ! authors = self.authors_from_one_paragraph(field) elif isinstance(field[1][0], nodes.bullet_list): ! authors = self.authors_from_bullet_list(field) else: raise TransformError else: ! authors = self.authors_from_paragraphs(field) authornodes = [nodes.author('', '', *author) for author in authors if author] *************** *** 328,332 **** raise ! def authorsfrom1paragraph(self, field): text = field[1][0].astext().strip() if not text: --- 340,344 ---- raise ! def authors_from_one_paragraph(self, field): text = field[1][0].astext().strip() if not text: *************** *** 340,344 **** return authors ! def authorsfrombulletlist(self, field): authors = [] for item in field[1][0]: --- 352,356 ---- return authors ! def authors_from_bullet_list(self, field): authors = [] for item in field[1][0]: *************** *** 350,354 **** return authors ! def authorsfromparagraphs(self, field): for item in field[1]: if not isinstance(item, nodes.paragraph): --- 362,366 ---- return authors ! def authors_from_paragraphs(self, field): for item in field[1]: if not isinstance(item, nodes.paragraph): |