From ec6300c841cbf7bbd7fb0dbac0707a6caac4dbaf Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sat, 19 Mar 2011 16:28:49 +0000 Subject: [PATCH] Quickbook: further improvements for simple markup. [SVN r70196] --- src/actions.cpp | 24 +++- src/actions.hpp | 6 - src/actions_class.cpp | 6 +- src/actions_class.hpp | 6 +- src/main_grammar.cpp | 110 ++++++++---------- test/Jamfile.v2 | 1 + ...fail-mismatched-boostbook-escape.quickbook | 5 + test/simple_markup.gold | 5 +- test/simple_markup.quickbook | 5 +- 9 files changed, 83 insertions(+), 85 deletions(-) create mode 100644 test/fail-mismatched-boostbook-escape.quickbook diff --git a/src/actions.cpp b/src/actions.cpp index e8361bd..8ee3611 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "quickbook.hpp" #include "actions.hpp" #include "utils.hpp" @@ -311,18 +312,31 @@ namespace quickbook { if(!actions.output_pre(out)) return; - out << pre; - std::string str(first, last); + char mark = *first; + int tag = + mark == '*' ? phrase_tags::bold : + mark == '/' ? phrase_tags::italic : + mark == '_' ? phrase_tags::underline : + mark == '=' ? phrase_tags::teletype : + 0; + + assert(tag != 0); + detail::markup markup = detail::markups[tag]; + + std::string str( + boost::next(first.base()), + boost::prior(last.base())); + + out << markup.pre; if (std::string const* ptr = find(macro, str.c_str())) { out << *ptr; } else { - while (first != last) - detail::print_char(*first++, out.get()); + detail::print_string(str, out.get()); } - out << post; + out << markup.post; } bool cond_phrase_push::start() diff --git a/src/actions.hpp b/src/actions.hpp index 42f1522..99d7619 100644 --- a/src/actions.hpp +++ b/src/actions.hpp @@ -136,21 +136,15 @@ namespace quickbook simple_phrase_action( collector& out - , std::string const& pre - , std::string const& post , string_symbols const& macro , quickbook::actions& actions) : out(out) - , pre(pre) - , post(post) , macro(macro) , actions(actions) {} void operator()(iterator first, iterator last) const; collector& out; - std::string pre; - std::string post; string_symbols const& macro; quickbook::actions& actions; }; diff --git a/src/actions_class.cpp b/src/actions_class.cpp index 98065c7..2df8482 100644 --- a/src/actions_class.cpp +++ b/src/actions_class.cpp @@ -77,11 +77,7 @@ namespace quickbook , raw_char(phrase, *this) , escape_unicode(phrase, *this) - , simple_bold(phrase, bold_pre_, bold_post_, macro, *this) - , simple_italic(phrase, italic_pre_, italic_post_, macro, *this) - , simple_underline(phrase, underline_pre_, underline_post_, macro, *this) - , simple_teletype(phrase, teletype_pre_, teletype_post_, macro, *this) - , simple_strikethrough(phrase, strikethrough_pre_, strikethrough_post_, macro, *this) + , simple_markup(phrase, macro, *this) , break_(phrase, *this) , do_macro(phrase, *this) diff --git a/src/actions_class.hpp b/src/actions_class.hpp index e3702b1..5182da3 100644 --- a/src/actions_class.hpp +++ b/src/actions_class.hpp @@ -122,11 +122,7 @@ namespace quickbook raw_char_action raw_char; escape_unicode_action escape_unicode; - 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; + simple_phrase_action simple_markup; break_action break_; do_macro_action do_macro; diff --git a/src/main_grammar.cpp b/src/main_grammar.cpp index 7bd937a..643ea57 100644 --- a/src/main_grammar.cpp +++ b/src/main_grammar.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -26,47 +27,6 @@ namespace quickbook { namespace cl = boost::spirit::classic; - template - inline void - simple_markup( - Rule& simple - , char mark - , Action const& action - , Rule const& close - ) - { - simple = - mark - >> lookback - [ cl::anychar_p - >> ~cl::eps_p(mark) // first mark not be preceeded by - // the same character. - >> (cl::space_p | cl::punct_p | cl::end_p) - // first mark must be preceeded - // by space or punctuation or the - // mark character or a the start. - ] - >> ( cl::graph_p // graph_p must follow first mark - >> *( cl::anychar_p - - ( lookback[cl::graph_p] - // final mark must be preceeded by - // graph_p - >> mark - >> ~cl::eps_p(mark) // final mark not be followed by - // the same character. - >> (cl::space_p | cl::punct_p | cl::end_p) - // final mark must be followed by - // space or punctuation - | close // Make sure that we don't go - // past a single block - ) - ) - >> cl::eps_p(mark) - ) [action] - >> mark - ; - } - struct main_grammar_local { struct assign_element_type { @@ -141,11 +101,9 @@ namespace quickbook top_level, blocks, paragraph_separator, code, code_line, blank_line, hr, list, list_item, element, - simple_phrase_end, escape, - inline_code, simple_format, - simple_bold, simple_italic, simple_underline, - simple_teletype, template_, + inline_code, + template_, code_block, macro, template_args, template_args_1_4, template_arg_1_4, @@ -157,6 +115,15 @@ namespace quickbook dummy_block ; + struct simple_markup_closure + : cl::closure + { + member1 mark; + }; + + cl::rule + simple_markup, simple_markup_end; + element_info::type_enum element_type; cl::rule element_rule; value::tag_type element_tag; @@ -293,7 +260,7 @@ namespace quickbook | local.break_ | local.code_block | local.inline_code - | local.simple_format + | local.simple_markup | local.escape | comment ; @@ -413,23 +380,40 @@ namespace quickbook ) ; - local.simple_format = - local.simple_bold - | local.simple_italic - | local.simple_underline - | local.simple_teletype + local.simple_markup = + ( cl::chset<>("*/_=") [local.simple_markup.mark = ph::arg1] + >> lookback + [ cl::anychar_p + >> ~cl::eps_p(cl::f_ch_p(local.simple_markup.mark)) + // first mark not be preceeded by + // the same character. + >> (cl::space_p | cl::punct_p | cl::end_p) + // first mark must be preceeded + // by space or punctuation or the + // mark character or a the start. + ] + >> cl::graph_p // graph_p must follow first mark + >> *(cl::anychar_p - local.simple_markup_end(local.simple_markup.mark)) + >> cl::f_ch_p(local.simple_markup.mark) + ) [actions.simple_markup] ; - local.simple_phrase_end = '[' | phrase_end; - - simple_markup(local.simple_bold, - '*', actions.simple_bold, local.simple_phrase_end); - simple_markup(local.simple_italic, - '/', actions.simple_italic, local.simple_phrase_end); - simple_markup(local.simple_underline, - '_', actions.simple_underline, local.simple_phrase_end); - simple_markup(local.simple_teletype, - '=', actions.simple_teletype, local.simple_phrase_end); + local.simple_markup_end + = ( lookback[cl::graph_p] // final mark must be preceeded by + // graph_p + >> cl::f_ch_p(local.simple_markup_end.mark) + >> ~cl::eps_p(cl::f_ch_p(local.simple_markup_end.mark)) + // final mark not be followed by + // the same character. + >> (cl::space_p | cl::punct_p | cl::end_p) + // final mark must be followed by + // space or punctuation + ) + | '[' + | "'''" + | '`' + | phrase_end + ; phrase = actions.scoped_context(element_info::in_phrase) @@ -479,7 +463,9 @@ namespace quickbook | ( ("'''" >> !eol) [actions.escape_pre] >> *(cl::anychar_p - "'''") [actions.raw_char] - >> cl::str_p("'''") [actions.escape_post] + >> ( cl::str_p("'''") [actions.escape_post] + | cl::eps_p [actions.error("Unclosed boostbook escape.")] + ) ) ; diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8bc6071..6244958 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -78,6 +78,7 @@ test-suite quickbook.test : [ quickbook-error-test fail-unknown-quickbook-1 ] [ quickbook-error-test fail-unknown-quickbook-2 ] [ quickbook-error-test fail-unknown-quickbook-3 ] + [ quickbook-error-test fail-mismatched-boostbook-escape ] [ quickbook-test utf-8 ] [ quickbook-test utf-8-bom ] [ quickbook-test unicode-escape ] diff --git a/test/fail-mismatched-boostbook-escape.quickbook b/test/fail-mismatched-boostbook-escape.quickbook new file mode 100644 index 0000000..1f9d0c0 --- /dev/null +++ b/test/fail-mismatched-boostbook-escape.quickbook @@ -0,0 +1,5 @@ +[article Badly escaped boostbook +[quickbook 1.5] +] + +'''something \ No newline at end of file diff --git a/test/simple_markup.gold b/test/simple_markup.gold index 3b912de..9cdca67 100644 --- a/test/simple_markup.gold +++ b/test/simple_markup.gold @@ -17,7 +17,7 @@ not__underlined__hopefully - (bold) + (bold) und/er/lined all/italic * not bold* @@ -28,5 +28,8 @@ not_underlined_ + + _Should not underline escaped markup_. _or this escaped_ markup form. + diff --git a/test/simple_markup.quickbook b/test/simple_markup.quickbook index abbaaa0..c275d23 100644 --- a/test/simple_markup.quickbook +++ b/test/simple_markup.quickbook @@ -10,7 +10,7 @@ not__underlined__hopefully -(*bold*) +(*bold*) _und/er/lined_ /all/italic/ * not bold* @@ -18,4 +18,7 @@ not__underlined__hopefully not_underlined_ +_Should not underline '''escaped''' markup_. +_or this '''escaped_ markup''' form. + [endsect] \ No newline at end of file