Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53045 - in branches/release: . tools/quickbook tools/quickbook/detail tools/quickbook/test
From: daniel_james_at_[hidden]
Date: 2009-05-16 09:48:55


Author: danieljames
Date: 2009-05-16 09:48:54 EDT (Sat, 16 May 2009)
New Revision: 53045
URL: http://svn.boost.org/trac/boost/changeset/53045

Log:
Import Python and C code snippets in quickbook.

Merged revisions 53018-53022 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r53018 | danieljames | 2009-05-15 07:12:13 +0100 (Fri, 15 May 2009) | 1 line
  
  Import Python and C code snippets in quickbook, by Jaroslav Gresula.
........
  r53019 | danieljames | 2009-05-15 07:12:42 +0100 (Fri, 15 May 2009) | 1 line
  
  Use '.py' when checking for python files.
........
  r53020 | danieljames | 2009-05-15 07:12:59 +0100 (Fri, 15 May 2009) | 1 line
  
  Use the correct source type for snippets.
........
  r53021 | danieljames | 2009-05-15 07:13:17 +0100 (Fri, 15 May 2009) | 1 line
  
  Separate code snippet grammar and actions.
........
  r53022 | danieljames | 2009-05-15 07:13:34 +0100 (Fri, 15 May 2009) | 1 line
  
  Make code_snippet_action language ignorant.
........

Added:
   branches/release/tools/quickbook/test/stub.c
      - copied unchanged from r53022, /trunk/tools/quickbook/test/stub.c
   branches/release/tools/quickbook/test/stub.py
      - copied unchanged from r53022, /trunk/tools/quickbook/test/stub.py
Properties modified:
   branches/release/ (props changed)
Text files modified:
   branches/release/tools/quickbook/code_snippet.hpp | 151 ++++++++++++++++++++++++++++++++-------
   branches/release/tools/quickbook/detail/actions.cpp | 32 +++++--
   branches/release/tools/quickbook/test/import.gold | 56 ++++++++++++++
   branches/release/tools/quickbook/test/import.quickbook | 6 +
   4 files changed, 208 insertions(+), 37 deletions(-)

Modified: branches/release/tools/quickbook/code_snippet.hpp
==============================================================================
--- branches/release/tools/quickbook/code_snippet.hpp (original)
+++ branches/release/tools/quickbook/code_snippet.hpp 2009-05-16 09:48:54 EDT (Sat, 16 May 2009)
@@ -15,12 +15,114 @@
 
 namespace quickbook
 {
- struct cpp_code_snippet_grammar
- : grammar<cpp_code_snippet_grammar>
+ struct code_snippet_actions
     {
- cpp_code_snippet_grammar(std::vector<template_symbol>& storage, std::string const& doc_id)
+ code_snippet_actions(std::vector<template_symbol>& storage,
+ std::string const& doc_id,
+ char const* source_type)
             : storage(storage)
             , doc_id(doc_id)
+ , source_type(source_type)
+ {}
+
+ void pass_thru(iterator first, iterator last);
+ void escaped_comment(iterator first, iterator last);
+ void compile(iterator first, iterator last);
+ void callout(iterator first, iterator last, char const* role);
+ void inline_callout(iterator first, iterator last);
+ void line_callout(iterator first, iterator last);
+
+ std::string code;
+ std::string snippet;
+ std::string id;
+ std::vector<std::string> callouts;
+ std::vector<template_symbol>& storage;
+ std::string const doc_id;
+ char const* const source_type;
+ };
+
+ struct python_code_snippet_grammar
+ : grammar<python_code_snippet_grammar>
+ {
+ typedef code_snippet_actions actions_type;
+
+ python_code_snippet_grammar(actions_type & actions)
+ : actions(actions)
+ {}
+
+ template <typename Scanner>
+ struct definition
+ {
+ typedef code_snippet_actions actions_type;
+
+ definition(python_code_snippet_grammar const& self)
+ {
+ actions_type& actions = self.actions;
+
+ start_ =
+ +(
+ snippet [boost::bind(&actions_type::compile, &actions, _1, _2)]
+ | anychar_p
+ )
+ ;
+
+ identifier =
+ (alpha_p | '_') >> *(alnum_p | '_')
+ ;
+
+ snippet =
+ "#[" >> *space_p
+ >> identifier [assign_a(actions.id)]
+ >> (*(code_elements - "#]"))
+ >> "#]"
+ ;
+
+ code_elements =
+ escaped_comment
+ | ignore
+ | (anychar_p - "#]") [boost::bind(&actions_type::pass_thru, &actions, _1, _2)]
+ ;
+
+ ignore =
+ *blank_p >> "#<-"
+ >> (*(anychar_p - "#->"))
+ >> "#->" >> *blank_p >> eol_p
+ | "\"\"\"<-\"\"\""
+ >> (*(anychar_p - "\"\"\"->\"\"\""))
+ >> "\"\"\"->\"\"\""
+ | "\"\"\"<-"
+ >> (*(anychar_p - "->\"\"\""))
+ >> "->\"\"\""
+ ;
+
+ escaped_comment =
+ *space_p >> "#`"
+ >> ((*(anychar_p - eol_p))
+ >> eol_p) [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
+ | *space_p >> "\"\"\"`"
+ >> (*(anychar_p - "\"\"\"")) [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
+ >> "\"\"\""
+ ;
+ }
+
+ rule<Scanner>
+ start_, snippet, identifier, code_elements, escaped_comment,
+ inline_callout, line_callout, ignore;
+
+ rule<Scanner> const&
+ start() const { return start_; }
+ };
+
+ actions_type& actions;
+ };
+
+ struct cpp_code_snippet_grammar
+ : grammar<cpp_code_snippet_grammar>
+ {
+ typedef code_snippet_actions actions_type;
+
+ cpp_code_snippet_grammar(actions_type & actions)
+ : actions(actions)
         {}
 
         template <typename Scanner>
@@ -28,10 +130,11 @@
         {
             definition(cpp_code_snippet_grammar const& self)
             {
- typedef cpp_code_snippet_grammar self_type;
+ actions_type& actions = self.actions;
+
                 start_ =
                     +(
- snippet [boost::bind(&self_type::compile, &self, _1, _2)]
+ snippet [boost::bind(&actions_type::compile, &actions, _1, _2)]
                         | anychar_p
                     )
                     ;
@@ -41,10 +144,16 @@
                     ;
 
                 snippet =
- "//[" >> *space_p
- >> identifier [assign_a(self.id)]
- >> (*(code_elements - "//]"))
- >> "//]"
+ "//[" >> *space_p
+ >> identifier [assign_a(actions.id)]
+ >> (*(code_elements - "//]"))
+ >> "//]"
+ |
+ "/*[" >> *space_p
+ >> identifier [assign_a(actions.id)]
+ >> *space_p >> "*/"
+ >> (*(code_elements - "/*]*"))
+ >> "/*]*/"
                     ;
 
                 code_elements =
@@ -52,18 +161,18 @@
                     | ignore
                     | line_callout
                     | inline_callout
- | (anychar_p - "//]") [boost::bind(&self_type::pass_thru, &self, _1, _2)]
+ | (anychar_p - "//]" - "/*]*/") [boost::bind(&actions_type::pass_thru, &actions, _1, _2)]
                     ;
 
                 inline_callout =
                     "/*<"
- >> (*(anychar_p - ">*/")) [boost::bind(&self_type::inline_callout, &self, _1, _2)]
+ >> (*(anychar_p - ">*/")) [boost::bind(&actions_type::inline_callout, &actions, _1, _2)]
>> ">*/"
                     ;
 
                 line_callout =
                     "/*<<"
- >> (*(anychar_p - ">>*/")) [boost::bind(&self_type::line_callout, &self, _1, _2)]
+ >> (*(anychar_p - ">>*/")) [boost::bind(&actions_type::line_callout, &actions, _1, _2)]
>> ">>*/"
>> *space_p
                     ;
@@ -83,9 +192,9 @@
                 escaped_comment =
                         *space_p >> "//`"
>> ((*(anychar_p - eol_p))
- >> eol_p) [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+ >> eol_p) [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
                     | *space_p >> "/*`"
- >> (*(anychar_p - "*/")) [boost::bind(&self_type::escaped_comment, &self, _1, _2)]
+ >> (*(anychar_p - "*/")) [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
>> "*/"
                     ;
             }
@@ -98,19 +207,7 @@
             start() const { return start_; }
         };
 
- void pass_thru(iterator first, iterator last) const;
- void escaped_comment(iterator first, iterator last) const;
- void compile(iterator first, iterator last) const;
- void callout(iterator first, iterator last, char const* role) const;
- void inline_callout(iterator first, iterator last) const;
- void line_callout(iterator first, iterator last) const;
-
- mutable std::string code;
- mutable std::string snippet;
- mutable std::string id;
- mutable std::vector<std::string> callouts;
- std::vector<template_symbol>& storage;
- std::string doc_id;
+ actions_type& actions;
     };
 }
 

Modified: branches/release/tools/quickbook/detail/actions.cpp
==============================================================================
--- branches/release/tools/quickbook/detail/actions.cpp (original)
+++ branches/release/tools/quickbook/detail/actions.cpp 2009-05-16 09:48:54 EDT (Sat, 16 May 2009)
@@ -968,7 +968,7 @@
         out << "\" />\n";
     }
 
- void cpp_code_snippet_grammar::pass_thru(iterator first, iterator last) const
+ void code_snippet_actions::pass_thru(iterator first, iterator last)
     {
         code += *first;
     }
@@ -978,7 +978,7 @@
         int callout_id = 0;
     }
 
- void cpp_code_snippet_grammar::callout(iterator first, iterator last, char const* role) const
+ void code_snippet_actions::callout(iterator first, iterator last, char const* role)
     {
         using detail::callout_id;
         code += "``'''";
@@ -993,24 +993,26 @@
         callouts.push_back(std::string(first, last));
     }
 
- void cpp_code_snippet_grammar::inline_callout(iterator first, iterator last) const
+ void code_snippet_actions::inline_callout(iterator first, iterator last)
     {
         callout(first, last, "callout_bug");
     }
 
- void cpp_code_snippet_grammar::line_callout(iterator first, iterator last) const
+ void code_snippet_actions::line_callout(iterator first, iterator last)
     {
         callout(first, last, "line_callout_bug");
     }
 
- void cpp_code_snippet_grammar::escaped_comment(iterator first, iterator last) const
+ void code_snippet_actions::escaped_comment(iterator first, iterator last)
     {
         if (!code.empty())
         {
             detail::unindent(code); // remove all indents
             if (code.size() != 0)
             {
- snippet += "\n\n``\n" + code + "``\n\n";
+ snippet += "\n\n";
+ snippet += source_type;
+ snippet += "``\n" + code + "``\n\n";
                 code.clear();
             }
         }
@@ -1022,7 +1024,7 @@
         }
     }
 
- void cpp_code_snippet_grammar::compile(iterator first, iterator last) const
+ void code_snippet_actions::compile(iterator first, iterator last)
     {
         using detail::callout_id;
         if (!code.empty())
@@ -1030,7 +1032,9 @@
             detail::unindent(code); // remove all indents
             if (code.size() != 0)
             {
- snippet += "\n\n```\n" + code + "```\n\n";
+ snippet += "\n\n";
+ snippet += source_type;
+ snippet += "```\n" + code + "```\n\n";
             }
 
             if(callouts.size() > 0)
@@ -1081,9 +1085,17 @@
         iterator_type first(code.begin(), code.end(), file);
         iterator_type last(code.end(), code.end());
 
- cpp_code_snippet_grammar g(storage, doc_id);
+ size_t fname_len = file.size();
+ bool is_python = fname_len >= 3
+ && file[--fname_len]=='y' && file[--fname_len]=='p' && file[--fname_len]=='.';
+ code_snippet_actions a(storage, doc_id, is_python ? "[python]" : "[c++]");
         // TODO: Should I check that parse succeeded?
- boost::spirit::classic::parse(first, last, g);
+ if(is_python) {
+ boost::spirit::classic::parse(first, last, python_code_snippet_grammar(a));
+ }
+ else {
+ boost::spirit::classic::parse(first, last, cpp_code_snippet_grammar(a));
+ }
 
         return 0;
     }

Modified: branches/release/tools/quickbook/test/import.gold
==============================================================================
--- branches/release/tools/quickbook/test/import.gold (original)
+++ branches/release/tools/quickbook/test/import.gold 2009-05-16 09:48:54 EDT (Sat, 16 May 2009)
@@ -32,4 +32,60 @@
 </programlisting>
     </para>
   </para>
+ <para>
+ <para>
+ This is the Python <emphasis role="bold"><emphasis>foo</emphasis></emphasis>
+ function.
+ </para>
+ <para>
+ This description can have paragraphs...
+ </para>
+ <itemizedlist>
+ <listitem>
+ lists
+ </listitem>
+ <listitem>
+ etc.
+ </listitem>
+ </itemizedlist>
+ <para>
+ And any quickbook block markup.
+ </para>
+ <para>
+
+<programlisting><phrase role="keyword">def</phrase> <phrase role="identifier">foo</phrase><phrase role="special">():</phrase>
+ <phrase role="comment"># return 'em, foo man!
+</phrase> <phrase role="keyword">return</phrase> <phrase role="string">&quot;foo&quot;</phrase>
+
+</programlisting>
+ </para>
+ </para>
+ <para>
+ <para>
+ This is the C <emphasis role="bold"><emphasis>foo</emphasis></emphasis> function.
+ </para>
+ <para>
+ This description can have paragraphs...
+ </para>
+ <itemizedlist>
+ <listitem>
+ lists
+ </listitem>
+ <listitem>
+ etc.
+ </listitem>
+ </itemizedlist>
+ <para>
+ And any quickbook block markup.
+ </para>
+ <para>
+
+<programlisting><phrase role="keyword">char</phrase><phrase role="special">*</phrase> <phrase role="identifier">foo</phrase><phrase role="special">()</phrase>
+<phrase role="special">{</phrase>
+ <phrase role="comment">// return 'em, foo man!
+</phrase> <phrase role="keyword">return</phrase> <phrase role="string">&quot;foo&quot;</phrase><phrase role="special">;</phrase>
+<phrase role="special">}</phrase>
+</programlisting>
+ </para>
+ </para>
 </article>

Modified: branches/release/tools/quickbook/test/import.quickbook
==============================================================================
--- branches/release/tools/quickbook/test/import.quickbook (original)
+++ branches/release/tools/quickbook/test/import.quickbook 2009-05-16 09:48:54 EDT (Sat, 16 May 2009)
@@ -1,6 +1,12 @@
 [article Import]
 
+[import stub.c]
+[import stub.py]
 [import stub.cpp]
 
 [foo]
 
+[foo_py]
+
+[foo_c]
+


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