From a26a70fc7b6c04a01986ef46a7102d4b1bc56f42 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Tue, 22 Nov 2011 23:45:16 +0000 Subject: [PATCH] Quickbook: Don't unindent code blocks in code snippets. Because code blocks are added in sections, a nested section could be unindented too far. Code is unindented in the main quickbook actions, so there's actually no need to do it when extracting code snippets. But this lead to left over whitespace that would have been stripped. So change the `start_snippet` and `end_snippet` grammars to swallow up the line they're on. I also added a newline before the end of the snippet, this cleaned up the occasional syntax error. But due to these changes there is more leading and trailing whitespace in the generated code blocks, so I just changed the code block grammar to strip them out. This means there are no newlines at the beginning or end of code blocks, but that should be fine. [SVN r75613] --- src/code_snippet.cpp | 62 ++++++++++++++++++++++++++-------- src/main_grammar.cpp | 16 ++++----- test/callouts-1_5.gold | 50 ++++++++------------------- test/code-1_1.gold | 9 ++--- test/include/code-import.gold | 13 +++---- test/include/code-include.gold | 3 +- test/quickbook_manual-1_4.gold | 12 +++---- test/snippets/pass_thru.gold | 10 ++---- 8 files changed, 85 insertions(+), 90 deletions(-) diff --git a/src/code_snippet.cpp b/src/code_snippet.cpp index 3c47d58..333b3e6 100644 --- a/src/code_snippet.cpp +++ b/src/code_snippet.cpp @@ -123,12 +123,19 @@ namespace quickbook ; start_snippet = - "#[" >> *cl::space_p - >> identifier [cl::assign_a(actions.id)] + *cl::blank_p + >> !(cl::eol_p >> *cl::blank_p) + >> "#[" + >> *cl::blank_p + >> identifier [cl::assign_a(actions.id)] + >> *(cl::anychar_p - cl::eol_p) ; end_snippet = - cl::str_p("#]") + *cl::blank_p + >> !(cl::eol_p >> *cl::blank_p) + >> "#]" + >> *(cl::anychar_p - cl::eol_p) ; ignore @@ -222,16 +229,45 @@ namespace quickbook ; start_snippet = - "//[" >> *cl::space_p - >> identifier [cl::assign_a(actions.id)] + *cl::blank_p + >> !(cl::eol_p >> *cl::blank_p) + >> "//[" + >> *cl::blank_p + >> identifier [cl::assign_a(actions.id)] + >> *(cl::anychar_p - cl::eol_p) | - "/*[" >> *cl::space_p - >> identifier [cl::assign_a(actions.id)] - >> *cl::space_p >> "*/" + *cl::blank_p + >> cl::eol_p + >> *cl::blank_p + >> "/*[" + >> *cl::space_p + >> identifier [cl::assign_a(actions.id)] + >> *cl::space_p + >> "*/" + >> *cl::blank_p + >> cl::eps_p(cl::eol_p) + | + "/*[" + >> *cl::space_p + >> identifier [cl::assign_a(actions.id)] + >> *cl::space_p + >> "*/" ; end_snippet = - cl::str_p("//]") | "/*]*/" + *cl::blank_p + >> !(cl::eol_p >> *cl::blank_p) + >> "//]" + >> *(cl::anychar_p - cl::eol_p) + | + *cl::blank_p + >> cl::eol_p + >> *cl::blank_p + >> "/*]*/" + >> *cl::blank_p + >> cl::eps_p(cl::eol_p) + | + "/*[*/" ; inline_callout @@ -345,8 +381,6 @@ namespace quickbook if (!code.empty()) { - detail::unindent(code); // remove all indents - if(snippet.content.empty()) { snippet.start_code = true; @@ -357,7 +391,7 @@ namespace quickbook snippet.content += source_type; snippet.content += "```\n"; } - + snippet.content += code; snippet.end_code = true; @@ -372,7 +406,7 @@ namespace quickbook if(snippet.end_code) { - snippet.content += "```\n\n"; + snippet.content += "\n```\n\n"; snippet.end_code = false; } } @@ -449,7 +483,7 @@ namespace quickbook } body += snippet->content; if(snippet->end_code) { - body += "```\n\n"; + body += "\n```\n\n"; } std::vector params; diff --git a/src/main_grammar.cpp b/src/main_grammar.cpp index 741e8ab..50d6860 100644 --- a/src/main_grammar.cpp +++ b/src/main_grammar.cpp @@ -551,20 +551,20 @@ namespace quickbook local.code_block = ( - "```" >> + "```" >> *(*cl::blank_p >> cl::eol_p) >> ( - *(cl::anychar_p - (*cl::blank_p >> "```")) - >> cl::eps_p(*cl::blank_p >> "```") + *(cl::anychar_p - (*cl::space_p >> "```")) + >> cl::eps_p(*cl::space_p >> "```") ) [actions.code_block] - >> *cl::blank_p >> "```" + >> *cl::space_p >> "```" ) | ( - "``" >> + "``" >> *(*cl::blank_p >> cl::eol_p) >> ( - *(cl::anychar_p - (*cl::blank_p >> "``")) - >> cl::eps_p(*cl::blank_p >> "``") + *(cl::anychar_p - (*cl::space_p >> "``")) + >> cl::eps_p(*cl::space_p >> "``") ) [actions.code_block] - >> *cl::blank_p >> "``" + >> *cl::space_p >> "``" ) ; diff --git a/test/callouts-1_5.gold b/test/callouts-1_5.gold index 51bef92..329b445 100644 --- a/test/callouts-1_5.gold +++ b/test/callouts-1_5.gold @@ -12,9 +12,7 @@ int roll_die() { boost::uniform_int<> dist(1, 6); -} - - +} @@ -29,9 +27,7 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -} - - +} @@ -48,9 +44,7 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -} - - +} @@ -67,9 +61,7 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -} - - +} @@ -86,10 +78,8 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -boost::uniform_int<> dist(1, 6); -} - - + boost::uniform_int<> dist(1, 6); +} @@ -109,8 +99,7 @@ -boost::uniform_int<> dist(1, 6); - +boost::uniform_int<> dist(1, 6); @@ -135,9 +124,7 @@ int roll_die() { boost::uniform_int<> dist(1, 6); -} - - +} @@ -152,9 +139,7 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -} - - +} @@ -171,9 +156,7 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -} - - +} @@ -190,9 +173,7 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -} - - +} @@ -209,10 +190,8 @@ int roll_die() { boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist); -boost::uniform_int<> dist(1, 6); -} - - + boost::uniform_int<> dist(1, 6); +} @@ -232,8 +211,7 @@ -boost::uniform_int<> dist(1, 6); - +boost::uniform_int<> dist(1, 6); diff --git a/test/code-1_1.gold b/test/code-1_1.gold index d738259..037e2e2 100644 --- a/test/code-1_1.gold +++ b/test/code-1_1.gold @@ -28,15 +28,13 @@ { std::cout << "Hello, World!" << std::endl; return 0; -} - +}
Test 3 -print "\xfabln\xeck" - +print "\xfabln\xeck"
@@ -45,8 +43,7 @@ This isn't valid C++ but I think we should accept it; -std::cout<<"\xfabln\xeck"<<"\n"; - +std::cout<<"\xfabln\xeck"<<"\n";
diff --git a/test/include/code-import.gold b/test/include/code-import.gold index 2f22097..ed62313 100644 --- a/test/include/code-import.gold +++ b/test/include/code-import.gold @@ -28,8 +28,7 @@ { // return 'em, foo man! return "foo"; -} - +} This is the Python foo @@ -56,9 +55,7 @@ def foo(): # return 'em, foo man! - return "foo" - - + return "foo" This is the C foo function. @@ -86,8 +83,7 @@ { // return 'em, foo man! return "foo"; -} - +} class x @@ -111,8 +107,7 @@ { n = n_; } -}; - +}; diff --git a/test/include/code-include.gold b/test/include/code-include.gold index 8b3a06d..9b9156c 100644 --- a/test/include/code-include.gold +++ b/test/include/code-include.gold @@ -31,8 +31,7 @@ { // return 'em, foo man! return "foo"; -} - +} This should appear when stub.py is included. diff --git a/test/quickbook_manual-1_4.gold b/test/quickbook_manual-1_4.gold index ff75786..4d4e12f 100644 --- a/test/quickbook_manual-1_4.gold +++ b/test/quickbook_manual-1_4.gold @@ -733,8 +733,7 @@ And one for the little boy who lives down the lane. { std::cout << "Hello, World!" << std::endl; return 0; -} - +}
@@ -2465,8 +2464,7 @@ for the journey to old age.]]] { std::cout << "Hello, World!" << std::endl; return 0; -} - +} @@ -2627,8 +2625,7 @@ for the journey to old age.]]] { // return 'em, foo man! return "foo"; -} - +} This is the bar function @@ -2701,8 +2698,7 @@ for the journey to old age.]]] std::string foo_bar() { return "foo-bar"; -} - +} diff --git a/test/snippets/pass_thru.gold b/test/snippets/pass_thru.gold index 91597d7..bba8526 100644 --- a/test/snippets/pass_thru.gold +++ b/test/snippets/pass_thru.gold @@ -10,8 +10,7 @@ Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -=============================================================================*/ - +=============================================================================*/ struct Foo{ @@ -24,13 +23,10 @@ int main() { Foo x; -} - - +} def foo: - print('foo') - + print('foo')