Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75895 - in branches/quickbook-dev/tools/quickbook: doc src test
From: dnljms_at_[hidden]
Date: 2011-12-11 06:14:21


Author: danieljames
Date: 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
New Revision: 75895
URL: http://svn.boost.org/trac/boost/changeset/75895

Log:
Quickbook: Support single entity source mode marker.
Added:
   branches/quickbook-dev/tools/quickbook/test/source_mode-1_7.gold (contents, props changed)
   branches/quickbook-dev/tools/quickbook/test/source_mode-1_7.quickbook (contents, props changed)
Text files modified:
   branches/quickbook-dev/tools/quickbook/doc/1_6.qbk | 28 ++++++++++++++++++++++++++++
   branches/quickbook-dev/tools/quickbook/src/actions.cpp | 27 +++++++++++++++++++++++++--
   branches/quickbook-dev/tools/quickbook/src/actions_class.cpp | 1 +
   branches/quickbook-dev/tools/quickbook/src/actions_class.hpp | 3 ++-
   branches/quickbook-dev/tools/quickbook/src/main_grammar.cpp | 14 ++++++++++++++
   branches/quickbook-dev/tools/quickbook/src/phrase_element_grammar.cpp | 11 ++++++++++-
   branches/quickbook-dev/tools/quickbook/src/phrase_tags.hpp | 1 +
   branches/quickbook-dev/tools/quickbook/src/string_ref.cpp | 7 +++++++
   branches/quickbook-dev/tools/quickbook/src/string_ref.hpp | 11 +++++++++++
   branches/quickbook-dev/tools/quickbook/test/Jamfile.v2 | 1 +
   10 files changed, 100 insertions(+), 4 deletions(-)

Modified: branches/quickbook-dev/tools/quickbook/doc/1_6.qbk
==============================================================================
--- branches/quickbook-dev/tools/quickbook/doc/1_6.qbk (original)
+++ branches/quickbook-dev/tools/quickbook/doc/1_6.qbk 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -302,4 +302,32 @@
 
 [endsect]
 
+[section:source-mode Source mode for single entities]
+
+1.7 introduces a new `!` element type for setting the source mode of a single
+entity without changing the source mode otherwise. This can be used for
+code blocks and other elements. For example:
+
+```
+[!c++]
+ void foo() {};
+
+[!python]``\`\`\`\ ``def foo():``\`\`\`\ ``
+```
+
+It can also be used to set the source mode for elements:
+
+[!teletype][table
+ [[code][meaning]]
+ [[`+`][addition]]
+]
+
+When used a section, it's only set for the section element, not the
+whole section.
+
+Currently it does support other syntactic entities such as paragraphs
+and lists. I'm not sure if it would be a good idea.
+
+[endsect]
+
 [endsect] [/ Quickbok 1.7]

Modified: branches/quickbook-dev/tools/quickbook/src/actions.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/actions.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/actions.cpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -45,6 +45,15 @@
     namespace {
         void write_anchors(quickbook::actions& actions, collector& tgt)
         {
+ // TODO: This works but is a bit of an odd place to put it.
+ // Might need to redefine the purpose of this function.
+ if (!actions.source_mode_next.empty()) {
+ detail::outwarn(actions.current_file, actions.source_mode_next.begin())
+ << "Temporary source mode unsupported here."
+ << std::endl;
+ actions.source_mode_next.clear();
+ }
+
             for(quickbook::actions::string_list::iterator
                 it = actions.anchors.begin(),
                 end = actions.anchors.end();
@@ -89,6 +98,7 @@
     void footnote_action(quickbook::actions&, value);
     void raw_phrase_action(quickbook::actions&, value);
     void source_mode_action(quickbook::actions&, value);
+ void next_source_mode_action(quickbook::actions&, value);
     void code_action(quickbook::actions&, value);
     void do_template_action(quickbook::actions&, value, string_iterator);
     
@@ -174,6 +184,8 @@
         case source_mode_tags::python:
         case source_mode_tags::teletype:
             return source_mode_action(actions, v);
+ case code_tags::next_source_mode:
+ return next_source_mode_action(actions, v);
         case code_tags::code_block:
         case code_tags::inline_code_block:
         case code_tags::inline_code:
@@ -591,6 +603,13 @@
         actions.source_mode = source_mode_tags::name(source_mode.get_tag());
     }
 
+ void next_source_mode_action(quickbook::actions& actions, value source_mode)
+ {
+ value_consumer values = source_mode;
+ actions.source_mode_next = values.consume().get_quickbook();
+ values.finish();
+ }
+
     void code_action(quickbook::actions& actions, value code_block)
     {
         int code_tag = code_block.get_tag();
@@ -603,6 +622,10 @@
             (code_tag == code_tags::inline_code_block && qbk_version_n < 106u);
         bool block = code_tag != code_tags::inline_code;
 
+ std::string source_mode = actions.source_mode_next.empty() ?
+ actions.source_mode : actions.source_mode_next;
+ actions.source_mode_next.clear();
+
         if (inline_code) {
             write_anchors(actions, actions.phrase);
         }
@@ -630,7 +653,7 @@
 
             // print the code with syntax coloring
             std::string str = syntax_highlight(first_, last_, actions,
- actions.source_mode);
+ source_mode);
 
             boost::swap(actions.current_file, saved_file);
 
@@ -647,7 +670,7 @@
             parse_iterator first_(code_value.begin());
             parse_iterator last_(code_value.end());
             std::string str = syntax_highlight(first_, last_, actions,
- actions.source_mode);
+ source_mode);
 
             actions.phrase << "<code>";
             actions.phrase << str;

Modified: branches/quickbook-dev/tools/quickbook/src/actions_class.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/actions_class.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/actions_class.cpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -36,6 +36,7 @@
         , imported(false)
         , macro()
         , source_mode("c++")
+ , source_mode_next()
         , current_file(0)
         , filename_relative(filein_.filename())
 

Modified: branches/quickbook-dev/tools/quickbook/src/actions_class.hpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/actions_class.hpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/actions_class.hpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -51,7 +51,8 @@
         bool imported;
         string_symbols macro;
         std::string source_mode;
- file_ptr current_file;
+ string_ref source_mode_next;
+ file_ptr current_file;
         fs::path filename_relative; // for the __FILENAME__ macro.
                                                     // (relative to the original file
                                                     // or include path).

Modified: branches/quickbook-dev/tools/quickbook/src/main_grammar.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/main_grammar.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/main_grammar.cpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -108,6 +108,14 @@
                     l.actions_.paragraph();
 
                 l.actions_.values.builder.reset();
+
+ if (!l.actions_.source_mode_next.empty() &&
+ info_.type != element_info::maybe_block)
+ {
+ l.actions_.source_mode.swap(saved_source_mode_);
+ l.actions_.source_mode = l.actions_.source_mode_next;
+ l.actions_.source_mode_next.clear();
+ }
                 
                 return true;
             }
@@ -125,8 +133,14 @@
             void success(parse_iterator, parse_iterator) { l.element_type = info_.type; }
             void failure() { l.element_type = element_info::nothing; }
 
+ void cleanup() {
+ if (!saved_source_mode_.empty())
+ l.actions_.source_mode.swap(saved_source_mode_);
+ }
+
             main_grammar_local& l;
             element_info info_;
+ std::string saved_source_mode_;
         };
 
         struct in_list_impl {

Modified: branches/quickbook-dev/tools/quickbook/src/phrase_element_grammar.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/phrase_element_grammar.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/phrase_element_grammar.cpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -27,7 +27,7 @@
     {
         cl::rule<scanner>
                         image, anchor, link, empty, cond_phrase, inner_phrase,
- role
+ role, source_mode
                         ;
     };
 
@@ -157,6 +157,15 @@
             ("footnote", element_info(element_info::phrase, &local.inner_phrase, phrase_tags::footnote))
             ;
 
+ elements.add("!", element_info(element_info::maybe_block, &local.source_mode, code_tags::next_source_mode, 107u))
+ ;
+
+ local.source_mode =
+ ( cl::str_p("c++")
+ | "python"
+ | "teletype"
+ ) [actions.values.entry(ph::arg1, ph::arg2)];
+
         elements.add
             ("c++", element_info(element_info::phrase, &local.empty, source_mode_tags::cpp))
             ("python", element_info(element_info::phrase, &local.empty, source_mode_tags::python))

Modified: branches/quickbook-dev/tools/quickbook/src/phrase_tags.hpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/phrase_tags.hpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/phrase_tags.hpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -35,6 +35,7 @@
         (code_block)
         (inline_code)
         (inline_code_block)
+ (next_source_mode)
     )
 }
 

Modified: branches/quickbook-dev/tools/quickbook/src/string_ref.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/string_ref.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/string_ref.cpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -9,10 +9,17 @@
 #include "string_ref.hpp"
 #include <boost/range/algorithm/equal.hpp>
 #include <boost/range/algorithm/lexicographical_compare.hpp>
+#include <boost/utility/swap.hpp>
 #include <ostream>
 
 namespace quickbook
 {
+ void string_ref::swap(string_ref& x)
+ {
+ boost::swap(begin_, x.begin_);
+ boost::swap(end_, x.end_);
+ }
+
     bool operator==(string_ref const& x, string_ref const& y)
     {
         return boost::equal(x, y);

Modified: branches/quickbook-dev/tools/quickbook/src/string_ref.hpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/string_ref.hpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/string_ref.hpp 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -37,6 +37,12 @@
         explicit string_ref(std::string const& x)
             : begin_(x.begin()), end_(x.end()) {}
 
+ void swap(string_ref&);
+
+ void clear() {
+ begin_ = end_ = iterator();
+ }
+
         operator std::string() const {
             return std::string(begin_, end_);
         }
@@ -73,6 +79,11 @@
     {
         return x > string_ref(y);
     }
+
+ inline void swap(string_ref& x, string_ref& y)
+ {
+ x.swap(y);
+ }
 }
 
 #endif

Modified: branches/quickbook-dev/tools/quickbook/test/Jamfile.v2
==============================================================================
--- branches/quickbook-dev/tools/quickbook/test/Jamfile.v2 (original)
+++ branches/quickbook-dev/tools/quickbook/test/Jamfile.v2 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -83,6 +83,7 @@
     [ quickbook-test section-1_5-unclosed ]
     [ quickbook-test section-1_5 ]
     [ quickbook-test simple_markup-1_5 ]
+ [ quickbook-test source_mode-1_7 ]
     [ quickbook-test svg-1_1 ]
     [ quickbook-test table-1_3 ]
     [ quickbook-test table-1_5 ]

Added: branches/quickbook-dev/tools/quickbook/test/source_mode-1_7.gold
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/source_mode-1_7.gold 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE article PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
+<article id="source_mode_test" last-revision="DEBUG MODE Date: 2000/12/20 12:00:00 $"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+ <title>Source Mode Test</title>
+ <informaltable frame="all">
+ <tgroup cols="2">
+ <tbody>
+ <row>
+ <entry>
+<programlisting><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase></programlisting>
+ </entry>
+ <entry>
+<programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">foo</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase></programlisting>
+ </entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </informaltable>
+<programlisting>Plain text...</programlisting>
+ <para>
+ <code><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase
+ role="special">()</phrase> <phrase role="special">{}</phrase></code> but <code>plain
+ text</code>.
+ </para>
+ <itemizedlist>
+ <listitem>
+ <simpara>
+ Sadly this doesn't work.
+ </simpara>
+ </listitem>
+ <listitem>
+ <simpara>
+ <code>int main() {}</code>
+ </simpara>
+ </listitem>
+ </itemizedlist>
+</article>

Added: branches/quickbook-dev/tools/quickbook/test/source_mode-1_7.quickbook
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/source_mode-1_7.quickbook 2011-12-11 06:14:17 EST (Sun, 11 Dec 2011)
@@ -0,0 +1,12 @@
+[quickbook 1.7]
+[source-mode teletype]
+[article Source Mode Test]
+
+[!c++][table [[``int main() {}``][``void foo() {}``]]]
+``Plain text...``
+
+[!c++]`int main() {}` but `plain text`.
+
+[!c++]
+* Sadly this doesn't work.
+* `int main() {}`


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