Skip Menu |

This queue is for tickets about the Text-Restructured CPAN distribution.

Report information
The Basics
Id: 25160
Status: resolved
Worked: 10 min
Priority: 0/
Queue: Text-Restructured

People
Owner: Nobody in particular
Requestors: glasse [...] cs.rpi.edu
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.003025



Subject: Wishlist: text only
Date: Mon, 26 Feb 2007 13:34:22 -0500
To: bug-Text-Restructured [...] rt.cpan.org
From: Ethan Glasser-Camp <glasse [...] cs.rpi.edu>
Download signature.asc
application/pgp-signature 252b

Message body not shown because it is not plain text.

Hi, Prest is really cool, but I'd like to use it as a part of another web app. Since the app already generates <body> tags and the rest of the layout for the site, I only need Prest to generate the formatted text. So I'd like to submit a wishlist item for a way to make prest and Restructured.pm generate only the main text. Attached is a simple patch to do what I think I want for HTML. I haven't tested it exhaustively, but when combined with: -W stylesheet=0 -D generator=0 -D time=0 -D source-link=0 it seems to do what I want. Let me know what you think. Thanks for your time! Ethan
diff -ur Text-Restructured-0.003024/lib/Text/Restructured/Writer/html.wrt hacked-Restructured/lib/Text/Restructured/Writer/html.wrt --- Text-Restructured-0.003024/lib/Text/Restructured/Writer/html.wrt 2007-02-21 14:23:10.000000000 -0500 +++ hacked-Restructured/lib/Text/Restructured/Writer/html.wrt 2007-02-26 13:16:52.000000000 -0500 @@ -1257,6 +1257,9 @@ # Handle the body. my $battr = $body_attr ? " $body_attr" : ''; my $body = [["<body$battr>\n"], ["</body>\n"]]; + if (defined $writer->{opt}{D}{text_only}){ + $body = [[], []]; + } unshift @{$body->[1]}, $FOOTER if defined $FOOTER; push @{$doc->[0]}, $body; push @{$body->[0]}, $HEADER if defined $HEADER; @@ -1267,8 +1270,8 @@ push (@{$body->[0]}, qq(<div class="document") . ($dom->{attr}{ids} ? qq( id="$dom->{attr}{ids}[0]") : "") - . qq(>\n)); - unshift (@{$body->[1]}, qq(</div>\n)); + . qq(>\n)) unless defined $writer->{opt}{D}{text_only}; + unshift (@{$body->[1]}, qq(</div>\n)) unless defined $writer->{opt}{D}{text_only}; push (@{$body->[0]}, qq(<h1 class="title">$title</h1>\n)) if $title ne '' && ! $writer->{opt}{D}{keep_title_section}; my $id = $dom->num_contents() > 1 && @@ -1290,7 +1293,14 @@ push (@{$body->[0]}, $content->{val}); } - my @list = Flatten($doc); + my @list; + if (defined $writer->{opt}{D}{text_only}){ + @list = Flatten($body); + } + else { + @list = Flatten($doc); + } + return join '',@list; # This subroutine takes an array of items which may
Subject: Re: [rt.cpan.org #25160] Wishlist: text only
Date: Mon, 26 Feb 2007 13:34:42 -0600
To: bug-Text-Restructured [...] rt.cpan.org
From: Mark Nodine <nodine [...] intrinsity.com>
Ethan Glasser-Camp via RT wrote: Show quoted text
> Mon Feb 26 13:41:01 2007: Request 25160 was acted upon. > Transaction: Ticket created by glasse@cs.rpi.edu > Queue: Text-Restructured > Subject: Wishlist: text only > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: glasse@cs.rpi.edu > Status: new > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=25160 > > > > Hi, > > Prest is really cool, but I'd like to use it as a part of another web > app. Since the app already generates <body> tags and the rest of the > layout for the site, I only need Prest to generate the formatted text. > So I'd like to submit a wishlist item for a way to make prest and > Restructured.pm generate only the main text.
I've found that there are typically a few other things that need to happen to use prest within another web app beyond just removing the body section. For example, attached is a ReStructuredText plugin for TWiki. Does your web app have a place where you can put glue code similar to a TWiki plugin? --Mark
# Plugin for TWiki Collaboration Platform, http://TWiki.org/ # # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it # Copyright (C) 2001-2004 Peter Thoeny, peter@thoeny.com # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # # ========================= # Each plugin is a package that may contain these functions: VERSION: # # earlyInitPlugin ( ) 1.020 # initPlugin ( $topic, $web, $user, $installWeb ) 1.000 # initializeUserHandler ( $loginName, $url, $pathInfo ) 1.010 # registrationHandler ( $web, $wikiName, $loginName ) 1.010 # beforeCommonTagsHandler ( $text, $topic, $web ) 1.024 # commonTagsHandler ( $text, $topic, $web ) 1.000 # afterCommonTagsHandler ( $text, $topic, $web ) 1.024 # startRenderingHandler ( $text, $web ) 1.000 # outsidePREHandler ( $text ) 1.000 # insidePREHandler ( $text ) 1.000 # endRenderingHandler ( $text ) 1.000 # beforeEditHandler ( $text, $topic, $web ) 1.010 # afterEditHandler ( $text, $topic, $web ) 1.010 # beforeSaveHandler ( $text, $topic, $web ) 1.010 # afterSaveHandler ( $text, $topic, $web, $errors ) 1.020 # writeHeaderHandler ( $query ) 1.010 Use only in one Plugin # redirectCgiQueryHandler ( $query, $url ) 1.010 Use only in one Plugin # getSessionValueHandler ( $key ) 1.010 Use only in one Plugin # setSessionValueHandler ( $key, $value ) 1.010 Use only in one Plugin # # initPlugin is required, all other are optional. # For increased performance, all handlers except initPlugin are # disabled. To enable a handler remove the leading DISABLE_ from # the function name. Remove disabled handlers you do not need. # # NOTE: To interact with TWiki use the official TWiki functions # in the TWiki::Func module. Do not reference any functions or # variables elsewhere in TWiki!! # ========================= # # This is the ReStructuredText TWiki plugin. # written by # Nicolas Tisserand (tisser_n@epita.fr), Nicolas Burrus (burrus_n@epita.fr) # and Perceval Anichini (anichi_p@epita.fr) # Modified for reStructuredText by Mark Nodine (nodine@freescale.com). # # It uses prest as HTML renderer for reStructuredText. # # Use it in your twiki text by writing %RESTSTART{tripopts}% ... %RESTEND% package TWiki::Plugins::ReStructuredTextPlugin; use IPC::Open2; # ========================= use vars qw( $web $topic $user $installWeb $VERSION $pluginName $debug $trip $tripoptions ); $VERSION = '1.000'; $pluginName = 'ReStructuredTextPlugin'; # Name of this Plugin # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if( $TWiki::Plugins::VERSION < 1.021 ) { TWiki::Func::writeWarning( "Version mismatch between ReStructuredTextPlugin and Plugins.pm" ); return 0; } # Get plugin debug flag $debug = TWiki::Func::getPluginPreferencesFlag( "DEBUG" ); # Get prest override flag $prest = TWiki::Func::getPluginPreferencesValue( "PREST" ) || '/usr/local/met/releases/ty/tools/bin/prest'; # Get prest override flag $prestoptions = TWiki::Func::getPluginPreferencesValue( "PRESTOPTIONS" ) || '-D source_link=0 -D time=0'; # Plugin correctly initialized TWiki::Func::writeDebug( "- TWiki::Plugins::ReStructuredTextPlugin::initPlugin( $web.$topic ) is OK" ) if $debug; return 1; } # ========================= sub pipeThru { my $out; my $pid = open2( \*Reader, \*Writer, $_[0]); print Writer $_[1]; close(Writer); while (<Reader>) { $out .= $_; } close (Reader); return $out; } # ========================= BEGIN { # Closure for the following variables my %Html2char = (amp => '&', lt => '<', gt => '>'); sub reST2html { my ($text, $opts) = @_; my %opts = $opts =~ /(\w+)="(.*?)"/g; my $secure = "-D trusted=0 -D file-insertion-enabled=0 -D raw-enabled=0"; # N.B.: To disable the security features, uncomment the following line $secure = ''; my $html = pipeThru("$prest $prestoptions $opts{options} $secure -- -", $text); if ($html =~ s/.*\<body\>\n(.*?)\n?\<\/body\>.*/$1/ios) { # Convert <PRE> tags to <VERBATIM> since TWiki does markup with <PRE> $html =~ s|(</?)pre\b|$1verbatim|gi; # Do special treatment on text outside of tags and verbatim my $in_verbatim = 0; $html = join '', map(do { # Postprocess WikiWords to quote them s|\b([A-Z][a-z]+[A-Z])|<nop>$1|g unless substr($_,0,1) eq '<' || $in_verbatim; # De-encode HTML tags within verbatim s/&(lt|gt|amp);/$Html2char{$1}/g if $in_verbatim; $in_verbatim = $in_verbatim && m|^</verbatim|i ? 0 : /^<verbatim/i ? 1 : $in_verbatim; $_ }, split /(<.*?>)/, $html); # Postprocess <a href> links into TWiki format #$html =~ s|<a [^>]*\bhref="(.*?)".*?>(.*?)</a>|[[$1][$2]]|gi; return ($opts{stylesheet} ? qq(<link rel="stylesheet" type="text/css" href="$opts{stylesheet}">\n) : '') . $html; } else { return "<font color=\"red\"> ReStructuredTextPlugin: internal error </font>\n<verbatim>\n$html\n</verbatim>\n"; } } } # ========================= sub commonTagsHandler { TWiki::Func::writeDebug( "- ReStructuredTextPlugin::commonTagsHandler( $_[2].$_[1] )" ) if $debug; # matches %RESTSTART{options}% ... %RESTEND% $_[0] =~ s/^%RESTSTART(?:\s*\{(.*?)\})?%\n(.*?)^%RESTEND%$/reST2html($2,$1)/megis; } # ========================= 1;
Subject: Re: [rt.cpan.org #25160] Wishlist: text only
Date: Tue, 27 Feb 2007 00:40:38 -0500
To: bug-Text-Restructured [...] rt.cpan.org
From: Ethan Glasser-Camp <glasse [...] cs.rpi.edu>
nodine@intrinsity.com via RT wrote: Show quoted text
>> Prest is really cool, but I'd like to use it as a part of another web >> app. Since the app already generates <body> tags and the rest of the >> layout for the site, I only need Prest to generate the formatted text. >> So I'd like to submit a wishlist item for a way to make prest and >> Restructured.pm generate only the main text.
> > I've found that there are typically a few other things that > need to happen to use prest within another web app beyond just > removing the body section. For example, attached is a > ReStructuredText plugin for TWiki. Does your web app have > a place where you can put glue code similar to a TWiki > plugin?
This is really helpful, thanks! The web app in question is ikiwiki (http://www.ikiwiki.info). Writing a plugin is doable, and I think I'll probably take much of the code from this plugin. A couple of comments: 1) Isn't forking a new process slower than just calling the same functions from your plugin? 2) Isn't it kind of a hack to just use regexes to strip out everything outside the body tags? Ethan
Download signature.asc
application/pgp-signature 252b

Message body not shown because it is not plain text.

On Mon Feb 26 13:41:01 2007, glasse@cs.rpi.edu wrote: Show quoted text
> Prest is really cool, but I'd like to use it as a part of another web > app. Since the app already generates <body> tags and the rest of the > layout for the site, I only need Prest to generate the formatted text. > So I'd like to submit a wishlist item for a way to make prest and > Restructured.pm generate only the main text.
The feature you have requested has been implemented in Text::Restructured 0.003025. Since it's a writer-specific flag, and since text-only did not describe the feature, it has been implemented using a "-W body-only" flag.
Show quoted text
> This is really helpful, thanks! > > The web app in question is ikiwiki (http://www.ikiwiki.info). Writing > a plugin is doable, and I think I'll probably take much of the code > from this plugin. > > A couple of comments: 1) Isn't forking a new process slower than just > calling the same functions from your plugin?
Yes. The plugin was written at a time when Text::Restructured was not structured well enough to be able to be called directly without using prest around it. Show quoted text
> 2) Isn't it kind of a > hack to just use regexes to strip out everything outside the body tags?
As I tried to mention before, in addition to removing the part within the body tags, it had to do the following additional processing: 1. Convert <pre> tags to <verbatim>. 2. Quote WikiWords that are not within a verbatim bock to suppress turning them into (probably broken) links. 3. Unencode encoded HTML entites for "<>&" within verbatim blocks. Since other processing is required beyond what "-W body-only" can provide, a wrapper was needed anyway, so it seemed just as easy to have the wrapper extract the body as to have the html writer do it. --Mark