|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r59310 - in branches/quickbook-1.5-spirit2: . detail
From: daniel_james_at_[hidden]
Date: 2010-01-27 17:03:53
Author: danieljames
Date: 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
New Revision: 59310
URL: http://svn.boost.org/trac/boost/changeset/59310
Log:
Xinclude/include/import.
Text files modified:
branches/quickbook-1.5-spirit2/block.cpp | 67 +++++++++-----
branches/quickbook-1.5-spirit2/block.hpp | 21 ++++
branches/quickbook-1.5-spirit2/block_actions.cpp | 176 +++++++++++++++++++++++++++++++++++++++
branches/quickbook-1.5-spirit2/detail/actions.cpp | 179 ----------------------------------------
branches/quickbook-1.5-spirit2/detail/actions.hpp | 36 --------
branches/quickbook-1.5-spirit2/detail/actions_class.cpp | 5 -
branches/quickbook-1.5-spirit2/detail/actions_class.hpp | 4
7 files changed, 240 insertions(+), 248 deletions(-)
Modified: branches/quickbook-1.5-spirit2/block.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/block.cpp (original)
+++ branches/quickbook-1.5-spirit2/block.cpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -93,6 +93,24 @@
(std::vector<quickbook::table_row>, rows)
)
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::xinclude,
+ (std::string, path)
+ (char const*, dummy)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::import,
+ (std::string, path)
+ (char const*, dummy)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::include,
+ (boost::optional<std::string>, id)
+ (std::string, path)
+)
+
namespace quickbook
{
namespace qi = boost::spirit::qi;
@@ -109,8 +127,8 @@
start_, blocks, block_markup,
space, blank, comment,
phrase, phrase_end, ordered_list,
- xinclude, include, hard_space, eol, paragraph_end,
- dummy_block, import;
+ hard_space, eol, paragraph_end,
+ dummy_block;
qi::symbols<> paragraph_end_markups;
qi::rule<iterator, quickbook::paragraph()> paragraph;
@@ -160,6 +178,10 @@
qi::rule<iterator, quickbook::code()> code;
qi::rule<iterator> code_line;
+
+ qi::rule<iterator, quickbook::xinclude()> xinclude;
+ qi::rule<iterator, quickbook::include()> include;
+ qi::rule<iterator, quickbook::import()> import;
qi::rule<iterator, quickbook::title()> title_phrase;
qi::rule<iterator, std::string()> phrase_attr;
@@ -242,9 +264,9 @@
| def_macro [actions.process][actions.output]
| table [actions.process][actions.output]
| variablelist [actions.process][actions.output]
- | xinclude
- | include
- | import
+ | xinclude [actions.process][actions.output]
+ | include [actions.process][actions.output]
+ | import [actions.process][actions.output]
| define_template [actions.process][actions.output]
)
>> ( (space >> ']' >> +eol)
@@ -467,31 +489,28 @@
;
xinclude =
- "xinclude"
- >> hard_space
- >> qi::raw[*(qi::char_ -
- phrase_end)] [actions.xinclude]
+ "xinclude"
+ >> hard_space
+ >> *(qi::char_ - phrase_end)
+ >> qi::attr("dummy")
;
import =
- "import"
- >> hard_space
- >> qi::raw[*(qi::char_ -
- phrase_end)] [actions.import]
+ "import"
+ >> hard_space
+ >> *(qi::char_ - phrase_end)
+ >> qi::attr("dummy")
;
include =
- "include"
- >> hard_space
- >>
- -(
- ':'
- >> qi::raw[*((qi::alnum | '_') - qi::space)]
- [ph::ref(actions.include_doc_id) = as_string(qi::_1)]
- >> space
- )
- >> qi::raw[*(qi::char_ -
- phrase_end)] [actions.include]
+ "include"
+ >> hard_space
+ >> -(
+ ':'
+ >> *((qi::alnum | '_') - qi::space)
+ >> space
+ )
+ >> *(qi::char_ - phrase_end)
;
code =
Modified: branches/quickbook-1.5-spirit2/block.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/block.hpp (original)
+++ branches/quickbook-1.5-spirit2/block.hpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -97,6 +97,24 @@
std::string title;
std::vector<table_row> rows;
};
+
+ struct xinclude
+ {
+ std::string path;
+ char const* dummy;
+ };
+
+ struct import
+ {
+ std::string path;
+ char const* dummy;
+ };
+
+ struct include
+ {
+ boost::optional<std::string> id;
+ std::string path;
+ };
void process(quickbook::actions&, hr);
void process(quickbook::actions&, paragraph const&);
@@ -108,6 +126,9 @@
void process(quickbook::actions&, define_template const&);
void process(quickbook::actions&, variablelist const&);
void process(quickbook::actions&, table const&);
+ void process(quickbook::actions&, xinclude const&);
+ void process(quickbook::actions&, import const&);
+ void process(quickbook::actions&, include const&);
}
#endif
\ No newline at end of file
Modified: branches/quickbook-1.5-spirit2/block_actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/block_actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/block_actions.cpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -12,7 +12,10 @@
#include "./detail/actions_class.hpp"
#include "./detail/markups.hpp"
#include "./detail/quickbook.hpp"
+#include "./code_snippet.hpp"
+#include <numeric>
#include <boost/assert.hpp>
+#include <boost/filesystem/convenience.hpp>
namespace quickbook
{
@@ -290,4 +293,177 @@
actions.phrase << "</informaltable>\n";
}
}
+
+ namespace
+ {
+ int load_snippets(
+ std::string const& file
+ , std::vector<template_symbol>& storage // snippets are stored in a
+ // vector of template_symbols
+ , std::string const& extension
+ , std::string const& doc_id)
+ {
+ std::string code;
+ int err = detail::load(file, code);
+ if (err != 0)
+ return err; // return early on error
+
+ iterator first(code.begin(), code.end(), file);
+ iterator last(code.end(), code.end());
+
+ size_t fname_len = file.size();
+ bool is_python = fname_len >= 3
+ && file[--fname_len]=='y' && file[--fname_len]=='p' && file[--fname_len]=='.';
+ code_snippet_actions a(storage, doc_id, is_python ? "[python]" : "[c++]");
+ // TODO: Should I check that parse succeeded?
+ if(is_python) {
+ python_code_snippet_grammar g(a);
+ boost::spirit::qi::parse(first, last, g);
+ }
+ else {
+ cpp_code_snippet_grammar g(a);
+ boost::spirit::qi::parse(first, last, g);
+ }
+
+ return 0;
+ }
+
+ fs::path include_search(fs::path const & current, std::string const & name)
+ {
+ fs::path path(name,fs::native);
+
+ // If the path is relative, try and resolve it.
+ if (!path.is_complete())
+ {
+ // See if it can be found locally first.
+ if (fs::exists(current / path))
+ {
+ return current / path;
+ }
+
+ // Search in each of the include path locations.
+ BOOST_FOREACH(std::string const & p, include_path)
+ {
+ fs::path full(p,fs::native);
+ full /= path;
+ if (fs::exists(full))
+ {
+ return full;
+ }
+ }
+ }
+
+ return path;
+ }
+
+ fs::path path_difference(fs::path const& outdir, fs::path const& path)
+ {
+ fs::path outtmp, temp;
+ fs::path::iterator out = outdir.begin(), file = path.begin();
+ for(; out != outdir.end() && file != path.end(); ++out, ++file)
+ {
+ if(!fs::equivalent(outtmp /= *out, temp /= *file))
+ break;
+ }
+ out = (out == outdir.begin()) ? outdir.end() : out;
+
+ fs::path result = fs::path();
+ for(; out != outdir.end(); ++out)
+ if(*out != ".") result /= "..";
+ std::divides<fs::path> concat;
+ return std::accumulate(file, path.end(), result, concat);
+ }
+
+ fs::path calculate_relative_path(std::string const& x, quickbook::actions& actions)
+ {
+ // Given a source file and the current filename, calculate the
+ // path to the source file relative to the output directory.
+ fs::path path(x);
+ if (!path.is_complete())
+ {
+ fs::path infile = fs::complete(actions.filename).normalize();
+ path = (infile.branch_path() / path).normalize();
+ fs::path outdir = fs::complete(actions.outdir).normalize();
+ path = path_difference(outdir, path);
+ }
+ return path;
+ }
+ }
+
+ void process(quickbook::actions& actions, xinclude const& x)
+ {
+ fs::path path = calculate_relative_path(x.path, actions);
+ actions.phrase << "\n<xi:include href=\"";
+ detail::print_string(detail::escape_uri(path.string()), actions.phrase.get());
+ actions.phrase << "\" />\n";
+ }
+
+ void process(quickbook::actions& actions, include const& x)
+ {
+ fs::path filein = include_search(actions.filename.branch_path(), x.path);
+ std::string doc_type, doc_id, doc_dirname, doc_last_revision;
+
+ // swap the filenames
+ std::swap(actions.filename, filein);
+
+ // save the doc info strings
+ actions.doc_type.swap(doc_type);
+ actions.doc_id.swap(doc_id);
+ actions.doc_dirname.swap(doc_dirname);
+ actions.doc_last_revision.swap(doc_last_revision);
+
+ // scope the macros
+ macro_symbols macro = actions.macro;
+ // scope the templates
+ //~ template_symbols templates = actions.templates; $$$ fixme $$$
+
+ // if an id is specified in this include (as in [include:id foo.qbk])
+ // then use it as the doc_id.
+ if (x.id) actions.doc_id = *x.id;
+
+ // update the __FILENAME__ macro
+ *actions.macro.find("__FILENAME__") =
+ quickbook::macro(actions.filename.native_file_string());
+
+ // parse the file
+ quickbook::parse(actions.filename.native_file_string().c_str(), actions, true);
+
+ // restore the values
+ std::swap(actions.filename, filein);
+
+ actions.doc_type.swap(doc_type);
+ actions.doc_id.swap(doc_id);
+ actions.doc_dirname.swap(doc_dirname);
+ actions.doc_last_revision.swap(doc_last_revision);
+
+ // restore the macros
+ actions.macro = macro;
+ // restore the templates
+ //~ actions.templates = templates; $$$ fixme $$$
+ }
+
+ void process(quickbook::actions& actions, import const& x)
+ {
+ fs::path path = include_search(actions.filename.branch_path(), x.path);
+ std::string ext = fs::extension(path);
+ std::vector<template_symbol> storage;
+ actions.error_count +=
+ load_snippets(path.string(), storage, ext, actions.doc_id);
+
+ BOOST_FOREACH(template_symbol const& ts, storage)
+ {
+ std::string tname = boost::get<0>(ts)[0];
+ if (actions.templates.find_top_scope(tname))
+ {
+ boost::spirit::classic::file_position const pos = boost::get<1>(ts);
+ detail::outerr(pos.file, pos.line)
+ << "Template Redefinition: " << tname << std::endl;
+ ++actions.error_count;
+ }
+ else
+ {
+ actions.templates.add(tname, ts);
+ }
+ }
+ }
}
\ No newline at end of file
Modified: branches/quickbook-1.5-spirit2/detail/actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.cpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -22,7 +22,6 @@
#include "./markups.hpp"
#include "./actions_class.hpp"
#include "../grammars.hpp"
-#include "../code_snippet.hpp"
namespace quickbook
{
@@ -147,48 +146,6 @@
detail::outwarn(pos.file,pos.line) << "Empty id.\n";
}
- fs::path path_difference(fs::path const& outdir, fs::path const& path)
- {
- fs::path outtmp, temp;
- fs::path::iterator out = outdir.begin(), file = path.begin();
- for(; out != outdir.end() && file != path.end(); ++out, ++file)
- {
- if(!fs::equivalent(outtmp /= *out, temp /= *file))
- break;
- }
- out = (out == outdir.begin()) ? outdir.end() : out;
-
- fs::path result = fs::path();
- for(; out != outdir.end(); ++out)
- if(*out != ".") result /= "..";
- std::divides<fs::path> concat;
- return std::accumulate(file, path.end(), result, concat);
- }
-
- fs::path calculate_relative_path(
- iterator first, iterator last, quickbook::actions& actions)
- {
- // Given a source file and the current filename, calculate the
- // path to the source file relative to the output directory.
- fs::path path(std::string(first, last));
- if (!path.is_complete())
- {
- fs::path infile = fs::complete(actions.filename).normalize();
- path = (infile.branch_path() / path).normalize();
- fs::path outdir = fs::complete(actions.outdir).normalize();
- path = path_difference(outdir, path);
- }
- return path;
- }
-
- void xinclude_action::operator()(iterator_range x, unused_type, unused_type) const
- {
- fs::path path = calculate_relative_path(x.begin(), x.end(), actions);
- out << "\n<xi:include href=\"";
- detail::print_string(detail::escape_uri(path.string()), out.get());
- out << "\" />\n";
- }
-
void code_snippet_actions::pass_thru(char x)
{
code += x;
@@ -290,142 +247,6 @@
id.clear();
}
- int load_snippets(
- std::string const& file
- , std::vector<template_symbol>& storage // snippets are stored in a
- // vector of template_symbols
- , std::string const& extension
- , std::string const& doc_id)
- {
- std::string code;
- int err = detail::load(file, code);
- if (err != 0)
- return err; // return early on error
-
- iterator first(code.begin(), code.end(), file);
- iterator last(code.end(), code.end());
-
- size_t fname_len = file.size();
- bool is_python = fname_len >= 3
- && file[--fname_len]=='y' && file[--fname_len]=='p' && file[--fname_len]=='.';
- code_snippet_actions a(storage, doc_id, is_python ? "[python]" : "[c++]");
- // TODO: Should I check that parse succeeded?
- if(is_python) {
- python_code_snippet_grammar g(a);
- boost::spirit::qi::parse(first, last, g);
- }
- else {
- cpp_code_snippet_grammar g(a);
- boost::spirit::qi::parse(first, last, g);
- }
-
- return 0;
- }
-
- namespace
- {
- fs::path include_search(fs::path const & current, std::string const & name)
- {
- fs::path path(name,fs::native);
-
- // If the path is relative, try and resolve it.
- if (!path.is_complete())
- {
- // See if it can be found locally first.
- if (fs::exists(current / path))
- {
- return current / path;
- }
-
- // Search in each of the include path locations.
- BOOST_FOREACH(std::string const & p, include_path)
- {
- fs::path full(p,fs::native);
- full /= path;
- if (fs::exists(full))
- {
- return full;
- }
- }
- }
-
- return path;
- }
- }
-
- void import_action::operator()(iterator_range x, unused_type, unused_type) const
- {
- fs::path path = include_search(actions.filename.branch_path(), std::string(x.begin(), x.end()));
- std::string ext = fs::extension(path);
- std::vector<template_symbol> storage;
- actions.error_count +=
- load_snippets(path.string(), storage, ext, actions.doc_id);
-
- BOOST_FOREACH(template_symbol const& ts, storage)
- {
- std::string tname = boost::get<0>(ts)[0];
- if (actions.templates.find_top_scope(tname))
- {
- boost::spirit::classic::file_position const pos = boost::get<1>(ts);
- detail::outerr(pos.file, pos.line)
- << "Template Redefinition: " << tname << std::endl;
- ++actions.error_count;
- }
- else
- {
- actions.templates.add(tname, ts);
- }
- }
- }
-
- void include_action::operator()(iterator_range x, unused_type, unused_type) const
- {
- fs::path filein = include_search(actions.filename.branch_path(), std::string(x.begin(), x.end()));
- std::string doc_type, doc_id, doc_dirname, doc_last_revision;
-
- // swap the filenames
- std::swap(actions.filename, filein);
-
- // save the doc info strings
- actions.doc_type.swap(doc_type);
- actions.doc_id.swap(doc_id);
- actions.doc_dirname.swap(doc_dirname);
- actions.doc_last_revision.swap(doc_last_revision);
-
- // scope the macros
- macro_symbols macro = actions.macro;
- // scope the templates
- //~ template_symbols templates = actions.templates; $$$ fixme $$$
-
- // if an id is specified in this include (as in [include:id foo.qbk])
- // then use it as the doc_id.
- if (!actions.include_doc_id.empty())
- {
- actions.doc_id = actions.include_doc_id;
- actions.include_doc_id.clear();
- }
-
- // update the __FILENAME__ macro
- *actions.macro.find("__FILENAME__") =
- quickbook::macro(actions.filename.native_file_string());
-
- // parse the file
- quickbook::parse(actions.filename.native_file_string().c_str(), actions, true);
-
- // restore the values
- std::swap(actions.filename, filein);
-
- actions.doc_type.swap(doc_type);
- actions.doc_id.swap(doc_id);
- actions.doc_dirname.swap(doc_dirname);
- actions.doc_last_revision.swap(doc_last_revision);
-
- // restore the macros
- actions.macro = macro;
- // restore the templates
- //~ actions.templates = templates; $$$ fixme $$$
- }
-
void xml_author::operator()(std::pair<std::string, std::string> const& author) const
{
out << " <author>\n"
Modified: branches/quickbook-1.5-spirit2/detail/actions.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.hpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -204,42 +204,6 @@
void operator()(iterator_range, unused_type, unused_type) const;
};
- struct xinclude_action
- {
- // Handles XML includes
- xinclude_action(collector& out_, quickbook::actions& actions_)
- : out(out_), actions(actions_) {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
-
- collector& out;
- quickbook::actions& actions;
- };
-
- struct include_action
- {
- // Handles QBK includes
-
- include_action(quickbook::actions& actions_)
- : actions(actions_) {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
-
- quickbook::actions& actions;
- };
-
- struct import_action
- {
- // Handles import of source code files (e.g. *.cpp *.py)
- import_action(collector& out_, quickbook::actions& actions_)
- : out(out_), actions(actions_) {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
-
- collector& out;
- quickbook::actions& actions;
- };
-
struct xml_author
{
// Handles xml author
Modified: branches/quickbook-1.5-spirit2/detail/actions_class.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.cpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -30,7 +30,6 @@
, doc_authors()
, doc_license()
, doc_last_revision()
- , include_doc_id()
, doc_license_1_1()
, doc_purpose_1_1()
@@ -68,10 +67,6 @@
, code(out, phrase, syntax_p)
, plain_char(phrase)
, raw_char(phrase)
-
- , xinclude(out, *this)
- , include(*this)
- , import(out, *this)
{
// turn off __FILENAME__ macro on debug mode = true
std::string filename_str = debug_mode ?
Modified: branches/quickbook-1.5-spirit2/detail/actions_class.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.hpp 2010-01-27 17:03:52 EST (Wed, 27 Jan 2010)
@@ -44,7 +44,6 @@
author_list doc_authors;
std::string doc_license;
std::string doc_last_revision;
- std::string include_doc_id;
std::string doc_license_1_1;
std::string doc_purpose_1_1;
@@ -102,9 +101,6 @@
raw_char_action raw_char;
element_id_warning_action element_id_warning;
- xinclude_action xinclude;
- include_action include;
- import_action import;
};
}
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