Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59300 - in branches/quickbook-1.5-spirit2: . detail
From: daniel_james_at_[hidden]
Date: 2010-01-27 17:01:58


Author: danieljames
Date: 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
New Revision: 59300
URL: http://svn.boost.org/trac/boost/changeset/59300

Log:
Escapes, simple markup, code.
Text files modified:
   branches/quickbook-1.5-spirit2/detail/actions.cpp | 15 ----
   branches/quickbook-1.5-spirit2/detail/actions.hpp | 17 ----
   branches/quickbook-1.5-spirit2/detail/actions_class.cpp | 12 ---
   branches/quickbook-1.5-spirit2/detail/actions_class.hpp | 12 ---
   branches/quickbook-1.5-spirit2/phrase.cpp | 145 ++++++++++++++++++++++-----------------
   branches/quickbook-1.5-spirit2/phrase.hpp | 13 +++
   branches/quickbook-1.5-spirit2/phrase_actions.cpp | 53 ++++++++++++++
   7 files changed, 148 insertions(+), 119 deletions(-)

Modified: branches/quickbook-1.5-spirit2/detail/actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.cpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -319,21 +319,6 @@
         out << "</programlisting>\n";
     }
 
- void inline_code_action::operator()(iterator_range x, unused_type, unused_type) const
- {
- std::string save;
- out.swap(save);
-
- // print the code with syntax coloring
- std::string str = syntax_p(x.begin(), x.end());
-
- out.swap(save);
-
- out << "<code>";
- out << str;
- out << "</code>";
- }
-
     void raw_char_action::operator()(char ch, unused_type, unused_type) const
     {
         phrase << ch;

Modified: branches/quickbook-1.5-spirit2/detail/actions.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.hpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -394,23 +394,6 @@
         syntax_highlight& syntax_p;
     };
 
- struct inline_code_action
- {
- // Does the actual syntax highlighing of code inlined in text
-
- inline_code_action(
- collector& out
- , syntax_highlight& syntax_p)
- : out(out)
- , syntax_p(syntax_p)
- {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
-
- collector& out;
- syntax_highlight& syntax_p;
- };
-
     struct start_varlistitem_action
     {
         start_varlistitem_action(collector& phrase)

Modified: branches/quickbook-1.5-spirit2/detail/actions_class.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.cpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -58,7 +58,6 @@
         , list_indent(-1)
         , template_info()
         , template_depth(0)
- , template_escape(false)
         , templates()
         , error_count(0)
 
@@ -72,8 +71,6 @@
 
         , syntax_p(source_mode, macro, *this)
         , code(out, phrase, syntax_p)
- , code_block(phrase, phrase, syntax_p)
- , inline_code(phrase, syntax_p)
         , paragraph(out, phrase, paragraph_pre, paragraph_post)
         , inside_paragraph(temp_para, phrase, paragraph_pre, paragraph_post)
         , h(out, phrase, doc_id, section_id, qualified_section_id, section_level)
@@ -99,12 +96,6 @@
         , list_format(list_buffer, list_indent, list_marks, error_count)
         , list_item(list_buffer, phrase, list_item_pre, list_item_post)
 
- , simple_bold(phrase, bold_pre_, bold_post_, macro)
- , simple_italic(phrase, italic_pre_, italic_post_, macro)
- , simple_underline(phrase, underline_pre_, underline_post_, macro)
- , simple_teletype(phrase, teletype_pre_, teletype_post_, macro)
- , simple_strikethrough(phrase, strikethrough_pre_, strikethrough_post_, macro)
-
         , variablelist(*this)
         , start_varlistentry(phrase, start_varlistentry_)
         , end_varlistentry(phrase, end_varlistentry_)
@@ -132,9 +123,6 @@
         , xinclude(out, *this)
         , include(*this)
         , import(out, *this)
-
- , escape_pre(phrase, escape_pre_)
- , escape_post(phrase, escape_post_)
     {
         // turn off __FILENAME__ macro on debug mode = true
         std::string filename_str = debug_mode ?

Modified: branches/quickbook-1.5-spirit2/detail/actions_class.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.hpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -85,7 +85,6 @@
         int list_indent;
         string_list template_info;
         int template_depth;
- bool template_escape;
         template_stack templates;
         int error_count;
 
@@ -105,8 +104,6 @@
 
         syntax_highlight syntax_p;
         code_action code;
- code_action code_block;
- inline_code_action inline_code;
         phrase_action paragraph;
         phrase_action inside_paragraph;
         generic_header_action h;
@@ -121,12 +118,6 @@
         list_format_action list_format;
         phrase_action list_item;
 
- simple_phrase_action simple_bold;
- simple_phrase_action simple_italic;
- simple_phrase_action simple_underline;
- simple_phrase_action simple_teletype;
- simple_phrase_action simple_strikethrough;
-
         variablelist_action variablelist;
         markup_action start_varlistentry;
         markup_action end_varlistentry;
@@ -155,9 +146,6 @@
         xinclude_action xinclude;
         include_action include;
         import_action import;
-
- markup_action escape_pre;
- markup_action escape_post;
     };
 }
 

Modified: branches/quickbook-1.5-spirit2/phrase.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.cpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -18,7 +18,6 @@
 #include <map>
 #include <boost/spirit/include/qi_core.hpp>
 #include <boost/spirit/include/qi_auxiliary.hpp>
-#include <boost/spirit/include/qi_attr.hpp>
 #include <boost/spirit/include/qi_symbols.hpp>
 #include <boost/spirit/repository/include/qi_confix.hpp>
 #include <boost/spirit/include/phoenix_core.hpp>
@@ -50,6 +49,19 @@
 )
 
 BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::simple_markup,
+ (char, symbol)
+ (std::string, raw_content)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::code,
+ (quickbook::file_position, position)
+ (std::string, code)
+ (bool, block)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
     quickbook::break_,
     (quickbook::file_position, position)
     (char const*, dummy)
@@ -101,8 +113,8 @@
                         space, blank, comment, phrase_markup,
                         phrase_end,
                         escape, common,
- hard_space, eol, inline_code, simple_format,
- code_block, replaceable, macro,
+ hard_space, eol,
+ replaceable, macro,
                         dummy_block,
                         brackets_1_4, template_inner_arg_1_5, brackets_1_5
                         ;
@@ -115,6 +127,7 @@
 
         qi::rule<iterator, std::string()> phrase;
         
+ qi::rule<iterator, quickbook::formatted()> escape_punct, escape_markup;
         qi::rule<iterator, quickbook::break_()> break_, escape_break;
 
         qi::rule<iterator, std::string()> macro_identifier;
@@ -125,7 +138,9 @@
         qi::rule<iterator, quickbook::image::attribute_map()> image_attributes;
         qi::rule<iterator, std::pair<std::string, std::string>()> image_attribute;
         
- qi::rule<iterator, boost::iterator_range<iterator>(char)> simple_markup;
+ qi::rule<iterator, quickbook::code()> inline_code, code_block;
+
+ qi::rule<iterator, quickbook::simple_markup(), qi::locals<char> > simple_format;
         
         qi::rule<iterator, quickbook::anchor()> anchor;
 
@@ -184,9 +199,9 @@
         common =
                 macro
             | phrase_markup
- | code_block
- | inline_code
- | simple_format
+ | code_block [actions.process]
+ | inline_code [actions.process]
+ | simple_format [actions.process]
             | escape
             | comment
             ;
@@ -237,66 +252,60 @@
             ;
 
         inline_code =
- '`' >>
- qi::raw[
- *(qi::char_ -
- ( '`'
- | (eol >> eol) // Make sure that we don't go
- ) // past a single block
- ) >> &qi::lit('`')
- ] [actions.inline_code]
+ '`'
+ >> position
+ >> qi::raw
+ [ *( qi::char_ -
+ ( '`'
+ | (eol >> eol) // Make sure that we don't go
+ ) // past a single block
+ )
+ >> &qi::lit('`')
+ ]
>> '`'
+ >> qi::attr(false)
             ;
 
         code_block =
                 (
- "```" >>
- qi::raw[
- *(qi::char_ - "```")
- >> &qi::lit("```")
- ] [actions.code_block]
- >> "```"
+ "```"
+ >> position
+ >> qi::raw[*(qi::char_ - "```")]
+ >> "```"
+ >> qi::attr(true)
                 )
             | (
- "``" >>
- qi::raw[
- *(qi::char_ - "``")
- >> &qi::lit("``")
- ] [actions.code_block]
- >> "``"
+ "``"
+ >> position
+ >> qi::raw[*(qi::char_ - "``")]
+ >> "``"
+ >> qi::attr(true)
                 )
             ;
 
- simple_markup =
- qi::omit[qi::char_(qi::_r1)] >>
- qi::raw[
- (
- qi::graph // A single char. e.g. *c*
- >> &(qi::char_(qi::_r1)
- >> (qi::space | qi::punct | qi::eoi))
- // space, punct or end
- ) // must follow qi::char_(qi::_r1)
- |
- ( qi::graph >> // qi::graph must follow qi::char_(qi::_r1)
- *(qi::char_ -
- ( (qi::graph >> qi::char_(qi::_r1)) // Make sure that we don't go
- | phrase_end // past a single block
+ simple_format %=
+ qi::char_("*/_=") [qi::_a = qi::_1]
+ >> qi::raw
+ [ ( ( qi::graph // A single char. e.g. *c*
+ >> &( qi::char_(qi::_a)
+ >> (qi::space | qi::punct | qi::eoi)
+ )
                         )
- ) >> qi::graph // qi::graph must precede qi::char_(qi::_r1)
- >> &(qi::char_(qi::_r1)
- >> (qi::space | qi::punct | qi::eoi))
- // space, punct or end
- ) // must follow qi::char_(qi::_r1)
- ]
- >> qi::omit[qi::char_(qi::_r1)]
- ;
-
-
- simple_format =
- simple_markup('*') [actions.simple_bold]
- | simple_markup('/') [actions.simple_italic]
- | simple_markup('_') [actions.simple_underline]
- | simple_markup('=') [actions.simple_teletype]
+ |
+ ( qi::graph // qi::graph must follow qi::lit(qi::_r1)
+ >> *( qi::char_ -
+ ( (qi::graph >> qi::lit(qi::_a))
+ | phrase_end // Make sure that we don't go
+ ) // past a single block
+ )
+ >> qi::graph // qi::graph must precede qi::lit(qi::_r1)
+ >> &( qi::char_(qi::_a)
+ >> (qi::space | qi::punct | qi::eoi)
+ )
+ )
+ )
+ ]
+ >> qi::omit[qi::char_(qi::_a)]
             ;
 
         phrase =
@@ -332,14 +341,11 @@
             ;
 
         escape =
- escape_break [actions.process]
+ ( escape_break
             | "\\ " // ignore an escaped char
- | '\\' >> qi::punct [actions.raw_char]
- | (
- ("'''" >> -eol) [actions.escape_pre]
- >> *(qi::char_ - "'''") [actions.raw_char]
- >> qi::lit("'''") [actions.escape_post]
- )
+ | escape_punct
+ | escape_markup
+ ) [actions.process]
             ;
         
         escape_break =
@@ -348,6 +354,19 @@
>> qi::attr("dummy")
             ;
 
+ escape_punct =
+ qi::attr(markup())
+ >> '\\'
+ >> qi::repeat(1)[qi::punct]
+ ;
+
+ escape_markup =
+ ("'''" >> -eol)
+ >> qi::attr(markup(escape_pre_, escape_post_))
+ >> *(qi::char_ - "'''")
+ >> "'''"
+ ;
+
         macro_identifier =
             +(qi::char_ - (qi::space | ']'))
             ;

Modified: branches/quickbook-1.5-spirit2/phrase.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.hpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.hpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -61,6 +61,17 @@
         std::string content;
     };
     
+ struct simple_markup {
+ char symbol;
+ std::string raw_content;
+ };
+
+ struct code {
+ bool block;
+ file_position position;
+ std::string code;
+ };
+
     struct cond_phrase {
         std::string macro_id;
         std::string content;
@@ -84,6 +95,8 @@
     void process(quickbook::actions&, anchor const&);
     void process(quickbook::actions&, link const&);
     void process(quickbook::actions&, formatted const&);
+ void process(quickbook::actions&, simple_markup const&);
+ void process(quickbook::actions&, code const&);
     void process(quickbook::actions&, cond_phrase const&);
     void process(quickbook::actions&, break_ const&);
     void process(quickbook::actions&, image const&);

Modified: branches/quickbook-1.5-spirit2/phrase_actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase_actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase_actions.cpp 2010-01-27 17:01:57 EST (Wed, 27 Jan 2010)
@@ -11,6 +11,7 @@
 #include "./phrase.hpp"
 #include "./detail/actions_class.hpp"
 #include "./detail/markups.hpp"
+#include <boost/assert.hpp>
 
 namespace quickbook
 {
@@ -39,6 +40,20 @@
         actions.phrase << x.type.pre << x.content << x.type.post;
     }
 
+ void process(quickbook::actions& actions, simple_markup const& x) {
+ markup type;
+ switch(x.symbol) {
+ case '*': type = markup(bold_pre_, bold_post_); break;
+ case '/': type = markup(italic_pre_, italic_post_); break;
+ case '_': type = markup(underline_pre_, underline_post_); break;
+ case '=': type = markup(teletype_pre_, teletype_post_); break;
+ default: BOOST_ASSERT(false);
+ }
+ actions.phrase << type.pre;
+ detail::print_string(x.raw_content, actions.phrase.get());
+ actions.phrase << type.post;
+ }
+
     void process(quickbook::actions& actions, cond_phrase const& x) {
         bool symbol_found = actions.macro.find(x.macro_id.c_str());
 
@@ -54,4 +69,42 @@
         actions.phrase << break_mark;
 
     }
+
+ void process(quickbook::actions& actions, code const& x) {
+ std::string program = x.code;
+
+ if(x.block) {
+ // preprocess the code section to remove the initial indentation
+ detail::unindent(program);
+ if (program.size() == 0)
+ return; // Nothing left to do here. The program is empty.
+ }
+
+ iterator first_(program.begin(), program.end());
+ iterator last_(program.end(), program.end());
+ first_.set_position(x.position);
+
+ std::string save;
+ actions.phrase.swap(save);
+
+ // print the code with syntax coloring
+ std::string str = actions.syntax_p(first_, last_);
+
+ actions.phrase.swap(save);
+
+ if(x.block) {
+ //
+ // We must not place a \n after the <programlisting> tag
+ // otherwise PDF output starts code blocks with a blank line:
+ //
+ actions.phrase << "<programlisting>";
+ actions.phrase << str;
+ actions.phrase << "</programlisting>\n";
+ }
+ else {
+ actions.phrase << "<code>";
+ actions.phrase << str;
+ actions.phrase << "</code>";
+ }
+ }
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk