1# Copyright 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from document_parser import ParseDocument 6 7 8class DocumentRenderer(object): 9 '''Performs document-level rendering such as the title and table of contents: 10 pulling that data out of the document, then replacing the $(title) and 11 $(table_of_contents) tokens with them. 12 13 This can be thought of as a parallel to TemplateRenderer; while 14 TemplateRenderer is responsible for interpreting templates and rendering files 15 within the template engine, DocumentRenderer is responsible for interpreting 16 higher-level document concepts like the title and TOC, then performing string 17 replacement for them. The syntax for this replacement is $(...) where ... is 18 the concept. Currently title and table_of_contents are supported. 19 ''' 20 21 def __init__(self, table_of_contents_renderer): 22 self._table_of_contents_renderer = table_of_contents_renderer 23 24 def Render(self, document, render_title=False): 25 parsed_document = ParseDocument(document, expect_title=render_title) 26 toc_text, toc_warnings = self._table_of_contents_renderer.Render( 27 parsed_document.sections) 28 29 # Only 1 title and 1 table of contents substitution allowed; in the common 30 # case, save necessarily running over the entire file. 31 if parsed_document.title: 32 document = document.replace('$(title)', parsed_document.title, 1) 33 return (document.replace('$(table_of_contents)', toc_text, 1), 34 parsed_document.warnings + toc_warnings) 35