Edgewall Software
Modify

Opened 19 years ago

Closed 18 years ago

Last modified 18 years ago

#1923 closed enhancement (fixed)

Use a better API to get just the HTML body from the reST parser

Reported by: Lele Gaifax Owned by: Matthew Good
Priority: normal Milestone: 0.10
Component: general Version: devel
Severity: normal Keywords:
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

The following patch use the newer publish_parts() API, introduced in Docutils 0.3.9, and knows about the upcoming 0.3.10 html_body part name.

diff -rN -u old-trac+darcs/trac/trac/mimeview/rst.py new-trac+darcs/trac/trac/mimeview/rst.py
--- old-trac+darcs/trac/trac/mimeview/rst.py    2005-08-14 21:41:13.000000000 +0200
+++ new-trac+darcs/trac/trac/mimeview/rst.py    2005-08-14 21:41:13.000000000 +0200
@@ -82,7 +82,7 @@
     def render(self, req, mimetype, content, filename=None, rev=None):
         try:
             from docutils import nodes
-            from docutils.core import publish_string
+            from docutils.core import publish_parts
             from docutils.parsers import rst
             from docutils import __version__
         except ImportError:
@@ -227,6 +227,11 @@
         _inliner = rst.states.Inliner()
         _parser = rst.Parser(inliner=_inliner)

-        html = publish_string(content, writer_name='html', parser=_parser,
+        parts = publish_parts(content, writer_name='html', parser=_parser,
                               settings_overrides={'halt_level': 6})
-        return html[html.find('<body>') + 6:html.find('</body>')].strip()
+        # Docutils 0.3.9 introduced html_body
+        if parts.has_key('html_body'):
+            html = parts['html_body']
+        else:
+            html = parts['body']
+        return html

Attachments (0)

Change History (5)

comment:1 by Jonas Borgström, 19 years ago

The patch looks good but I think we should wait past 0.9 before applying this patch because this requires the latest docutils version.

comment:2 by Lele, 19 years ago

Uhm, don't be scared by that comment, that effectively may be misleading: the publish_parts() API has been there since Docutils 0.3.3, released in 2004-05-09.

comment:3 by Lele, 19 years ago

Addendum

I propose also the following patch, that allows the configuration of language, input_encoding and output_encoding passed to the docutils engine. It takes the values from a new [docutils] section of TracIni, that the patch updates as well.

As I'm going to use Trac to wrap a Subversion repository that contains reStructuredText documentation written in italian, this is a needed capability.

Wed Aug 24 20:18:05 CEST 2005  lele@metapensiero.it
  * Fetch the default value for language and in/out encodings used by reST from {{{[docutils]}}} in {{{trac.ini}}}
diff -rN -u old-trac/trac/mimeview/rst.py new-trac/trac/mimeview/rst.py
--- old-trac/trac/mimeview/rst.py	2005-08-24 20:25:42.070333872 +0200
+++ new-trac/trac/mimeview/rst.py	2005-08-24 20:25:42.097329768 +0200
@@ -227,8 +227,16 @@
         _inliner = rst.states.Inliner()
         _parser = rst.Parser(inliner=_inliner)
 
+        language = self.config.get('trac', 'default_lang', 'en')
+        code = self.config.get('docutils', 'language_code', language)
+        charset = self.config.get('trac', 'default_charset')
+        inenc = self.config.get('docutils', 'input_encoding', charset)
+        outenc = self.config.get('docutils', 'output_encoding', charset)
         parts = publish_parts(content, writer_name='html', parser=_parser,
-                              settings_overrides={'halt_level': 6})
+                              settings_overrides={'halt_level': 6,
+                                                  'language_code': code,
+                                                  'input_encoding': inenc,
+                                                  'output_encoding': outenc})
         # Docutils 0.3.9 introduced html_body
         if parts.has_key('html_body'):
             html = parts['html_body']
diff -rN -u old-trac/wiki-default/TracIni new-trac/wiki-default/TracIni
--- old-trac/wiki-default/TracIni	2005-08-24 20:25:42.069334024 +0200
+++ new-trac/wiki-default/TracIni	2005-08-24 20:25:42.128325056 +0200
@@ -18,6 +18,7 @@
 || database        || Database to use for this project ||

 || templates_dir   || Path of Clearsilver templates ||

 || default_charset || Source files uses this charset ||

+|| default_lang    || Language code to use for templates and the like, defaults to ''en'' ||

 

 == [logging] ==

 || log_type  || Logging facility to use. (none, file, stderr, syslog, winlog) ||

@@ -64,6 +65,13 @@
 == [diff] ==

 || tab_width || Displayed tab width in changeset diffs ||

 

+== [docutils] ==

+|| language_code   || This specifies the language used by the reStructuredText engine, and defaults to {{{[trac].default_lang}}} ||

+|| input_encoding  || Encoding to be expected in input texts, by default {{{[trac].default_lang}}} ||

+|| output_encoding || Encoding of text produced by docutils, by default {{{[trac].default_lang}}} ||

+

+See also: WikiRestructuredText

+

 [[BR]]

 ----

 See also: TracGuide, TracAdmin
\ No newline at end of file

comment:4 by Matthew Good, 18 years ago

Milestone: 0.10
Owner: changed from Jonas Borgström to Matthew Good
Status: newassigned

Well, Trac requires Docutils 0.3.9 since [3512], so we can upgrade to the new APIs now.

comment:5 by Matthew Good, 18 years ago

Resolution: fixed
Status: assignedclosed

Ok, the API change was made in [3514]. The encoding shouldn't be relevant here since Trac now does everything in unicode and [3514] also removed the redundant UTF encoding/decoding since docutils already uses unicode. If there are other reasons for having encoding config options that should go into a new ticket.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Matthew Good.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Matthew Good to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.