Quickbook: Code block callouts.

[SVN r75897]
This commit is contained in:
Daniel James
2011-12-11 11:16:01 +00:00
parent d3d03dd760
commit 672caeec17
12 changed files with 510 additions and 160 deletions

View File

@@ -31,13 +31,13 @@ namespace quickbook
char const* source_type)
: last_code_pos(source_file->source.begin())
, in_code(false)
, callout_id(0)
, snippet_stack()
, storage(storage)
, source_file(source_file)
, source_type(source_type)
, error_count(0)
{
source_file->is_code_snippets = true;
content.start(source_file);
}
@@ -48,7 +48,6 @@ namespace quickbook
void start_snippet_impl(std::string const&, string_iterator);
void end_snippet(string_iterator first, string_iterator last);
void end_snippet_impl(string_iterator);
void callout(string_iterator first, string_iterator last);
void end_file(string_iterator, string_iterator);
void append_code(string_iterator first, string_iterator last);
@@ -56,26 +55,22 @@ namespace quickbook
struct snippet_data
{
snippet_data(std::string const& id, int callout_base_id)
snippet_data(std::string const& id)
: id(id)
, callout_base_id(callout_base_id)
, start_code(false)
{}
std::string id;
int callout_base_id;
bool start_code;
std::string::const_iterator source_pos;
mapped_file_builder::pos start_pos;
value_builder callouts;
boost::shared_ptr<snippet_data> next;
};
void push_snippet_data(std::string const& id, int callout_base_id,
void push_snippet_data(std::string const& id,
std::string::const_iterator pos)
{
boost::shared_ptr<snippet_data> new_snippet(
new snippet_data(id, callout_base_id));
boost::shared_ptr<snippet_data> new_snippet(new snippet_data(id));
new_snippet->next = snippet_stack;
snippet_stack = new_snippet;
snippet_stack->start_code = in_code;
@@ -95,7 +90,6 @@ namespace quickbook
std::string::const_iterator mark_begin, mark_end;
std::string::const_iterator last_code_pos;
bool in_code;
int callout_id;
boost::shared_ptr<snippet_data> snippet_stack;
std::vector<template_symbol>& storage;
file_ptr source_file;
@@ -240,8 +234,6 @@ namespace quickbook
| escaped_comment [boost::bind(&actions_type::escaped_comment, &actions, _1, _2)]
| ignore [boost::bind(&actions_type::append_code, &actions, _1, _2)]
| pass_thru_comment [boost::bind(&actions_type::pass_thru, &actions, _1, _2)]
| line_callout [boost::bind(&actions_type::callout, &actions, _1, _2)]
| inline_callout [boost::bind(&actions_type::callout, &actions, _1, _2)]
| cl::anychar_p
;
@@ -287,23 +279,6 @@ namespace quickbook
"/*[*/"
;
inline_callout
= cl::confix_p(
"/*<" >> *cl::space_p,
(*cl::anychar_p) [boost::bind(&actions_type::mark, &actions, _1, _2)],
">*/"
)
;
line_callout
= cl::confix_p(
"/*<<" >> *cl::space_p,
(*cl::anychar_p) [boost::bind(&actions_type::mark, &actions, _1, _2)],
">>*/"
)
>> *cl::space_p
;
ignore
= cl::confix_p(
*cl::blank_p >> "//<-",
@@ -354,7 +329,7 @@ namespace quickbook
cl::rule<Scanner>
start_, identifier, code_elements, start_snippet, end_snippet,
escaped_comment, pass_thru_comment, inline_callout, line_callout, ignore;
escaped_comment, pass_thru_comment, ignore;
cl::rule<Scanner> const&
start() const { return start_; }
@@ -447,27 +422,6 @@ namespace quickbook
content.add(mark_begin, mark_end);
}
void code_snippet_actions::callout(string_iterator first, string_iterator last)
{
if(!snippet_stack) return;
append_code(first, last);
if (!in_code)
{
content.add("\n\n", first);
content.add(source_type, first);
content.add("```\n", first);
in_code = true;
}
content.add(
"``[[callout" + boost::lexical_cast<std::string>(callout_id) + "]]``",
first);
snippet_stack->callouts.insert(qbk_value(source_file, mark_begin, mark_end, template_tags::block));
++callout_id;
}
void code_snippet_actions::escaped_comment(string_iterator first, string_iterator last)
{
append_code(first, last);
@@ -549,7 +503,7 @@ namespace quickbook
void code_snippet_actions::start_snippet_impl(std::string const& id,
string_iterator position)
{
push_snippet_data(id, callout_id, position);
push_snippet_data(id, position);
}
void code_snippet_actions::end_snippet_impl(string_iterator position)
@@ -557,7 +511,6 @@ namespace quickbook
assert(snippet_stack);
boost::shared_ptr<snippet_data> snippet = pop_snippet_data();
value callouts = snippet->callouts.release();
mapped_file_builder f;
f.start(source_file);
@@ -572,29 +525,11 @@ namespace quickbook
}
std::vector<std::string> params;
int i = 0;
for(value::iterator it = callouts.begin(); it != callouts.end(); ++it)
{
params.push_back("[callout" + boost::lexical_cast<std::string>(snippet->callout_base_id + i) + "]");
++i;
}
file_ptr body = f.release();
value_builder builder;
builder.set_tag(template_tags::snippet);
builder.insert(qbk_value(body, body->source.begin(), body->source.end(),
template_tags::block));
builder.insert(callouts);
template_symbol symbol(snippet->id, params, builder.release());
storage.push_back(symbol);
// Copy the snippet's callouts to its parent
if(snippet_stack)
{
snippet_stack->callouts.extend(callouts);
}
storage.push_back(template_symbol(snippet->id, params,
qbk_value(body, body->source.begin(), body->source.end(),
template_tags::snippet)));
}
}