Index: detail/actions.cpp =================================================================== --- detail/actions.cpp (revision 52998) +++ detail/actions.cpp (working copy) @@ -968,7 +968,7 @@ out << "\" />\n"; } - void cpp_code_snippet_grammar::pass_thru(iterator first, iterator last) const + void code_snippet_grammar::pass_thru(iterator first, iterator last) const { 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_grammar::callout(iterator first, iterator last, char const* role) const { using detail::callout_id; code += "``'''"; @@ -993,17 +993,17 @@ callouts.push_back(std::string(first, last)); } - void cpp_code_snippet_grammar::inline_callout(iterator first, iterator last) const + void code_snippet_grammar::inline_callout(iterator first, iterator last) const { callout(first, last, "callout_bug"); } - void cpp_code_snippet_grammar::line_callout(iterator first, iterator last) const + void code_snippet_grammar::line_callout(iterator first, iterator last) const { callout(first, last, "line_callout_bug"); } - void cpp_code_snippet_grammar::escaped_comment(iterator first, iterator last) const + void code_snippet_grammar::escaped_comment(iterator first, iterator last) const { if (!code.empty()) { @@ -1022,7 +1022,7 @@ } } - void cpp_code_snippet_grammar::compile(iterator first, iterator last) const + void code_snippet_grammar::compile(iterator first, iterator last) const { using detail::callout_id; if (!code.empty()) @@ -1081,7 +1081,9 @@ 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 = file[--fname_len]=='y' && file[--fname_len]=='p'; + code_snippet_grammar g(storage, doc_id, is_python); // TODO: Should I check that parse succeeded? boost::spirit::classic::parse(first, last, g); Index: code_snippet.hpp =================================================================== --- code_snippet.hpp (revision 52998) +++ code_snippet.hpp (working copy) @@ -15,20 +15,32 @@ namespace quickbook { - struct cpp_code_snippet_grammar - : grammar - { - cpp_code_snippet_grammar(std::vector& storage, std::string const& doc_id) + struct code_snippet_grammar + : grammar + { + code_snippet_grammar(std::vector& storage, + std::string const& doc_id, + bool is_python) : storage(storage) , doc_id(doc_id) + , is_python(is_python) {} template struct definition { - definition(cpp_code_snippet_grammar const& self) + typedef code_snippet_grammar self_type; + + definition(self_type const& self) { - typedef cpp_code_snippet_grammar self_type; + self.is_python + ? definition_python(self) + : definition_cpp(self) + ; + } + + void definition_python(self_type const& self) + { start_ = +( snippet [boost::bind(&self_type::compile, &self, _1, _2)] @@ -41,18 +53,72 @@ ; snippet = - "//[" >> *space_p + "#[" >> *space_p >> identifier [assign_a(self.id)] - >> (*(code_elements - "//]")) - >> "//]" + >> (*(code_elements - "#]")) + >> "#]" ; code_elements = escaped_comment | ignore + | (anychar_p - "#]") [boost::bind(&self_type::pass_thru, &self, _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(&self_type::escaped_comment, &self, _1, _2)] + | *space_p >> "\"\"\"`" + >> (*(anychar_p - "\"\"\"")) [boost::bind(&self_type::escaped_comment, &self, _1, _2)] + >> "\"\"\"" + ; + } + + void definition_cpp(self_type const& self) + { + start_ = + +( + snippet [boost::bind(&self_type::compile, &self, _1, _2)] + | anychar_p + ) + ; + + identifier = + (alpha_p | '_') >> *(alnum_p | '_') + ; + + snippet = + "//[" >> *space_p + >> identifier [assign_a(self.id)] + >> (*(code_elements - "//]")) + >> "//]" + | + "/*[" >> *space_p + >> identifier [assign_a(self.id)] + >> *space_p >> "*/" + >> (*(code_elements - "/*]*")) + >> "/*]*/" + ; + + code_elements = + escaped_comment + | ignore | line_callout | inline_callout - | (anychar_p - "//]") [boost::bind(&self_type::pass_thru, &self, _1, _2)] + | (anychar_p - "//]" - "/*]*/") [boost::bind(&self_type::pass_thru, &self, _1, _2)] ; inline_callout = @@ -111,7 +177,9 @@ mutable std::vector callouts; std::vector& storage; std::string doc_id; + bool is_python; }; + } #endif // BOOST_SPIRIT_QUICKBOOK_CODE_SNIPPET_HPP