Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59555 - branches/quickbook-1.5-spirit2
From: daniel_james_at_[hidden]
Date: 2010-02-07 04:09:06


Author: danieljames
Date: 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
New Revision: 59555
URL: http://svn.boost.org/trac/boost/changeset/59555

Log:
Don't include markup in the grammars, instead use a string to indicate
what type of markup is expected.
Added:
   branches/quickbook-1.5-spirit2/phrase_actions.hpp (contents, props changed)
Text files modified:
   branches/quickbook-1.5-spirit2/block.cpp | 26 ++++----
   branches/quickbook-1.5-spirit2/boostbook.cpp | 109 ++++++++++++++++++++++++++++++++++++++-
   branches/quickbook-1.5-spirit2/boostbook.hpp | 4 +
   branches/quickbook-1.5-spirit2/parse_types.hpp | 14 +----
   branches/quickbook-1.5-spirit2/phrase.cpp | 46 ++++++++--------
   branches/quickbook-1.5-spirit2/phrase.hpp | 2
   branches/quickbook-1.5-spirit2/phrase_actions.cpp | 29 +++++-----
   branches/quickbook-1.5-spirit2/process.cpp | 1
   8 files changed, 164 insertions(+), 67 deletions(-)

Modified: branches/quickbook-1.5-spirit2/block.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/block.cpp (original)
+++ branches/quickbook-1.5-spirit2/block.cpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -125,7 +125,7 @@
         qi::rule<iterator, quickbook::heading()> heading;
         qi::symbols<char, int> heading_symbol;
         qi::rule<iterator, quickbook::formatted()> paragraph_block, blockquote, preformatted;
- qi::symbols<char, quickbook::markup> paragraph_blocks;
+ qi::symbols<char, quickbook::formatted_type> paragraph_blocks;
         qi::rule<iterator, quickbook::def_macro()> def_macro;
         qi::rule<iterator, quickbook::table()> table;
         qi::rule<iterator, quickbook::table_row()> table_row;
@@ -242,18 +242,18 @@
             ;
 
         paragraph_blocks.add
- ("blurb", markup(blurb_pre, blurb_post))
- ("warning", markup(warning_pre, warning_post))
- ("caution", markup(caution_pre, caution_post))
- ("important", markup(important_pre, important_post))
- ("note", markup(note_pre, note_post))
- ("tip", markup(tip_pre, tip_post))
+ ("blurb", formatted_type("blurb"))
+ ("warning", formatted_type("warning"))
+ ("caution", formatted_type("caution"))
+ ("important", formatted_type("important"))
+ ("note", formatted_type("note"))
+ ("tip", formatted_type("tip"))
             ;
 
         blockquote =
                 ':'
>> blank
- >> qi::attr(markup(blockquote_pre, blockquote_post))
+ >> qi::attr(formatted_type("blockquote"))
>> inside_paragraph
             ;
 
@@ -261,7 +261,7 @@
                 "pre"
>> hard_space [ph::ref(no_eols) = false]
>> -eol
- >> qi::attr(markup(preformatted_pre, preformatted_post))
+ >> qi::attr(formatted_type("preformatted"))
>> phrase_attr
>> qi::eps [ph::ref(no_eols) = true]
             ;
@@ -301,7 +301,7 @@
             ;
 
         table_cell_body =
- qi::attr(markup(start_cell_, end_cell_))
+ qi::attr(formatted_type("cell"))
>> inside_paragraph
             ;
 
@@ -333,7 +333,7 @@
             ;
 
         varlistterm_body =
- qi::attr(markup(start_varlistterm_, end_varlistterm_))
+ qi::attr(formatted_type("varlistterm"))
>> phrase_attr
             ;
 
@@ -346,7 +346,7 @@
             ;
 
         varlistitem_body =
- qi::attr(markup(start_varlistitem_, end_varlistitem_))
+ qi::attr(formatted_type("varlistitem"))
>> inside_paragraph
             ;
 
@@ -500,7 +500,7 @@
             ;
 
         inside_paragraph2 =
- qi::attr(markup(paragraph_pre, paragraph_post))
+ qi::attr(formatted_type("paragraph"))
>> phrase_attr;
 
         phrase_attr =

Modified: branches/quickbook-1.5-spirit2/boostbook.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/boostbook.cpp (original)
+++ branches/quickbook-1.5-spirit2/boostbook.cpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -6,6 +6,107 @@
 
 namespace quickbook
 {
+ template <typename Iter>
+ std::string encode(Iter first, Iter last)
+ {
+ std::string r;
+
+ for(;first != last; ++first) {
+ switch (*first)
+ {
+ case '<': r += "&lt;"; break;
+ case '>': r += "&gt;"; break;
+ case '&': r += "&amp;"; break;
+ case '"': r += "&quot;"; break;
+ default: r += *first; break;
+ }
+ }
+
+ return r;
+ }
+
+ std::string encode(std::string const& x) {
+ return encode(x.begin(), x.end());
+ }
+
+ std::string encode(char const* x) {
+ char const* end = x;
+ while(*end) ++end;
+ return encode(x, end);
+ }
+
+ std::string encode(char c) {
+ return encode(&c, &c + 1);
+ }
+
+ namespace {
+ struct boostbook_markup {
+ char const* quickbook;
+ char const* pre;
+ char const* post;
+ };
+
+ boostbook_markup markups[] = {
+ { "", "", "" },
+ { "comment", "<!--", "-->" },
+ { "paragraph", "<para>\n", "</para>\n" },
+ { "h1", "<bridgehead renderas=\"sect1\">", "</bridgehead>" },
+ { "h2", "<bridgehead renderas=\"sect2\">", "</bridgehead>" },
+ { "h3", "<bridgehead renderas=\"sect3\">", "</bridgehead>" },
+ { "h4", "<bridgehead renderas=\"sect4\">", "</bridgehead>" },
+ { "h5", "<bridgehead renderas=\"sect5\">", "</bridgehead>" },
+ { "h6", "<bridgehead renderas=\"sect6\">", "</bridgehead>" },
+ { "blurb", "<sidebar role=\"blurb\">\n", "</sidebar>\n" },
+ { "blockquote", "<blockquote><para>", "</para></blockquote>" },
+ { "preformatted", "<programlisting>", "</programlisting>" },
+ { "warning", "<warning>", "</warning>" },
+ { "caution", "<caution>", "</caution>" },
+ { "important", "<important>", "</important>" },
+ { "note", "<note>", "</note>" },
+ { "tip", "<tip>", "</tip>" },
+ { "list_item", "<listitem>\n", "\n</listitem>" },
+ { "bold", "<emphasis role=\"bold\">", "</emphasis>" },
+ { "italic", "<emphasis>", "</emphasis>" },
+ { "underline", "<emphasis role=\"underline\">", "</emphasis>" },
+ { "teletype", "<literal>", "</literal>" },
+ { "strikethrough", "<emphasis role=\"strikethrough\">", "</emphasis>" },
+ { "quote", "<quote>", "</quote>" },
+ { "url", "<ulink url=\"", "</ulink>" },
+ { "link", "<link linkend=\"", "</link>" },
+ { "funcref", "<functionname alt=\"", "</functionname>" },
+ { "classref", "<classname alt=\"", "</classname>" },
+ { "memberref", "<methodname alt=\"", "</methodname>" },
+ { "enumref", "<enumname alt=\"", "</enumname>" },
+ { "macroref", "<macroname alt=\"", "</macroname>" },
+ { "headerref", "<headername alt=\"", "</headername>" },
+ { "conceptref", "<conceptname alt=\"", "</conceptname>" },
+ { "globalref", "<globalname alt=\"", "</globalname>" },
+ { "footnote", "<footnote><para>", "</para></footnote>" },
+ { "escape", "<!--quickbook-escape-prefix-->", "<!--quickbook-escape-postfix-->" },
+ { "replaceable", "<replaceable>", "</replaceable>" },
+ { "varlistentry", "<varlistentry>", "</varlistentry>\n" },
+ { "varlistterm", "<term>", "</term>" },
+ { "varlistitem", "<listitem>", "</listitem>" },
+ { "header", "<thead>", "</thead>\n" },
+ { "row", "<row>", "</row>\n" },
+ { "cell", "<entry>", "</entry>" },
+ { "programlisting", "<programlisting>", "</programlisting>\n" },
+ { "code", "<code>", "</code>" },
+ { "hr", "<para/>", "" },
+ { "break", "<sbr/>", "" },
+ };
+
+ std::map<std::string, boostbook_markup> markup_map;
+
+ struct initialize {
+ initialize() {
+ BOOST_FOREACH(boostbook_markup m, markups) {
+ markup_map[m.quickbook] = m;
+ }
+ }
+ } initialize_instance;
+ }
+
     void output(quickbook::actions& actions, std::string const& x)
     {
         actions.phrase << x;
@@ -18,14 +119,16 @@
     }
 
     void output(quickbook::actions& actions, link const& x) {
- actions.phrase << x.type.pre;
+ boostbook_markup m = markup_map.at(x.type);
+ actions.phrase << m.pre;
         detail::print_string(x.destination, actions.phrase.get());
         actions.phrase << "\">";
         actions.phrase << x.content;
- actions.phrase << x.type.post;
+ actions.phrase << m.post;
     }
 
     void output(quickbook::actions& actions, formatted const& x) {
- actions.phrase << x.type.pre << x.content << x.type.post;
+ boostbook_markup m = markup_map.at(x.type);
+ actions.phrase << m.pre << x.content << m.post;
     }
 }

Modified: branches/quickbook-1.5-spirit2/boostbook.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/boostbook.hpp (original)
+++ branches/quickbook-1.5-spirit2/boostbook.hpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -13,4 +13,8 @@
     void output(quickbook::actions&, anchor const&);
     void output(quickbook::actions&, link const&);
     void output(quickbook::actions&, formatted const&);
+
+ std::string encode(std::string const&);
+ std::string encode(char);
+ std::string encode(char const*);
 }

Modified: branches/quickbook-1.5-spirit2/parse_types.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/parse_types.hpp (original)
+++ branches/quickbook-1.5-spirit2/parse_types.hpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -16,25 +16,17 @@
 
 namespace quickbook
 {
- struct markup {
- markup()
- : pre(""), post("") {}
- markup(char const* pre, char const* post)
- : pre(pre), post(post) {}
-
- char const* pre;
- char const* post;
- };
+ typedef char const* formatted_type;
 
     struct formatted {
- markup type;
+ formatted_type type;
         std::string content;
     };
 }
 
 BOOST_FUSION_ADAPT_STRUCT(
     quickbook::formatted,
- (quickbook::markup, type)
+ (quickbook::formatted_type, type)
     (std::string, content)
 )
 

Modified: branches/quickbook-1.5-spirit2/phrase.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.cpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -33,7 +33,7 @@
 
 BOOST_FUSION_ADAPT_STRUCT(
     quickbook::link,
- (quickbook::markup, type)
+ (quickbook::formatted_type, type)
     (std::string, destination)
     (std::string, content)
 )
@@ -104,11 +104,11 @@
         qi::rule<iterator, std::string()> image_attribute_key, image_attribute_value;
         qi::rule<iterator, quickbook::link()> url;
         qi::rule<iterator, quickbook::link()> link;
- qi::symbols<char, markup> link_symbol;
+ qi::symbols<char, formatted_type> link_symbol;
         qi::rule<iterator, quickbook::anchor()> anchor;
         qi::symbols<char, quickbook::source_mode> source_mode;
         qi::rule<iterator, quickbook::formatted()> formatted;
- qi::symbols<char, markup> format_symbol;
+ qi::symbols<char, formatted_type> format_symbol;
         qi::rule<iterator, quickbook::formatted()> footnote;
         qi::rule<iterator, quickbook::call_template()> call_template;
         qi::rule<iterator, std::vector<std::string>() > template_args;
@@ -249,14 +249,14 @@
             ;
 
         escape_punct =
- qi::attr(markup())
+ qi::attr(formatted_type(""))
>> '\\'
>> qi::repeat(1)[qi::punct]
             ;
 
         escape_markup =
                 ("'''" >> -eol)
- >> qi::attr(markup(escape_pre_, escape_post_))
+ >> qi::attr("escape")
>> *(qi::char_ - "'''")
>> "'''"
             ;
@@ -324,7 +324,7 @@
 
         url =
                 '@'
- >> qi::attr(markup(url_pre_, url_post_))
+ >> qi::attr("url")
>> *(qi::char_ - (']' | qi::space))
>> ( &qi::lit(']')
                 | (hard_space >> phrase)
@@ -341,15 +341,15 @@
             ;
 
         link_symbol.add
- ("link", markup(link_pre_, link_post_))
- ("funcref", markup(funcref_pre_, funcref_post_))
- ("classref", markup(classref_pre_, classref_post_))
- ("memberref", markup(memberref_pre_, memberref_post_))
- ("enumref", markup(enumref_pre_, enumref_post_))
- ("macroref", markup(macroref_pre_, macroref_post_))
- ("headerref", markup(headerref_pre_, headerref_post_))
- ("conceptref", markup(conceptref_pre_, conceptref_post_))
- ("globalref", markup(globalref_pre_, globalref_post_))
+ ("link", formatted_type("link"))
+ ("funcref", formatted_type("funcref"))
+ ("classref", formatted_type("classref"))
+ ("memberref", formatted_type("memberref"))
+ ("enumref", formatted_type("enumref"))
+ ("macroref", formatted_type("macroref"))
+ ("headerref", formatted_type("headerref"))
+ ("conceptref", formatted_type("conceptref"))
+ ("globalref", formatted_type("globalref"))
             ;
 
         anchor =
@@ -368,18 +368,18 @@
         formatted = format_symbol >> blank >> phrase;
 
         format_symbol.add
- ("*", markup(bold_pre_, bold_post_))
- ("'", markup(italic_pre_, italic_post_))
- ("_", markup(underline_pre_, underline_post_))
- ("^", markup(teletype_pre_, teletype_post_))
- ("-", markup(strikethrough_pre_, strikethrough_post_))
- ("\"", markup(quote_pre_, quote_post_))
- ("~", markup(replaceable_pre_, replaceable_post_))
+ ("*", "bold")
+ ("'", "italic")
+ ("_", "underline")
+ ("^", "teletype")
+ ("-", "strikethrough")
+ ("\"", "quote")
+ ("~", "replaceable")
             ;
 
         footnote =
                 "footnote"
- >> qi::attr(markup(footnote_pre_, footnote_post_))
+ >> qi::attr("footnote")
>> blank
>> phrase
             ;

Modified: branches/quickbook-1.5-spirit2/phrase.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.hpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.hpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -37,7 +37,7 @@
     };
     
     struct link {
- markup type;
+ formatted_type type;
         std::string destination;
         std::string content;
     };

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-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -14,6 +14,7 @@
 #include "markups.hpp"
 #include "utils.hpp"
 #include "code.hpp"
+#include "boostbook.hpp"
 
 namespace quickbook
 {
@@ -23,18 +24,17 @@
     }
 
     std::string process(quickbook::actions& actions, macro const& x) {
- // TODO: Should the dates be encoded?
         if (x.raw_markup == quickbook_get_date)
         {
             char strdate[64];
             strftime(strdate, sizeof(strdate), "%Y-%b-%d", current_time);
- return strdate;
+ return encode(strdate);
         }
         else if (x.raw_markup == quickbook_get_time)
         {
             char strdate[64];
             strftime(strdate, sizeof(strdate), "%I:%M:%S %p", current_time);
- return strdate;
+ return encode(strdate);
         }
         else
         {
@@ -45,25 +45,24 @@
     link process(quickbook::actions& actions, link const& x) {
         link r = x;
         if(r.content.empty()) {
- // TODO: Encode this
- r.content = x.destination;
+ r.content = encode(x.destination);
         }
         return r;
     }
 
- nothing process(quickbook::actions& actions, simple_markup const& x) {
- markup type;
+ formatted process(quickbook::actions& actions, simple_markup const& x) {
+ formatted r;
         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;
+ case '*': r.type = "bold"; break;
+ case '/': r.type = "italic"; break;
+ case '_': r.type = "underline"; break;
+ case '=': r.type = "teletype"; break;
             default: BOOST_ASSERT(false);
         }
- actions.phrase << type.pre;
- detail::print_string(x.raw_content, actions.phrase.get());
- actions.phrase << type.post;
- return nothing();
+
+ r.content = encode(x.raw_content);
+
+ return r;
     }
 
     nothing process(quickbook::actions& actions, cond_phrase const& x) {

Added: branches/quickbook-1.5-spirit2/phrase_actions.hpp
==============================================================================
--- (empty file)
+++ branches/quickbook-1.5-spirit2/phrase_actions.hpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -0,0 +1,9 @@
+#include "phrase.hpp"
+
+namespace quickbook
+{
+ nothing process(quickbook::actions&, source_mode const&);
+ std::string process(quickbook::actions&, macro const&);
+ link process(quickbook::actions&, link const&);
+ formatted process(quickbook::actions&, simple_markup const&);
+}

Modified: branches/quickbook-1.5-spirit2/process.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/process.cpp (original)
+++ branches/quickbook-1.5-spirit2/process.cpp 2010-02-07 04:09:04 EST (Sun, 07 Feb 2010)
@@ -38,7 +38,6 @@
 
     void output(quickbook::actions&, std::string const&);
     nothing process(quickbook::actions&, call_template const&);
- nothing process(quickbook::actions&, simple_markup const&);
     nothing process(quickbook::actions&, cond_phrase const&);
     nothing process(quickbook::actions&, break_ const&);
     nothing process(quickbook::actions&, image const&);


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