mono-docs: Allow embedding examples in XML format

XML examples must provide their own <example> and <code> tags, and these
tags MUST be escaped: \< \> \" etc.
This is more inconvenient, but it allows adding explanatory text outside
the <code> and inside the <example>.
Examples are first looked for in XML format, and if not found, in CS format.
This commit is contained in:
Xavi Artigas 2019-09-17 12:02:19 +02:00
parent 24ed24a9db
commit 4673120a19
1 changed files with 21 additions and 7 deletions

View File

@ -367,20 +367,34 @@ struct documentation_generator
auto options = efl::eolian::grammar::context_find_tag<options_context>(context);
// Example embedding not requested
if (options.examples_dir.empty()) return true;
std::string file_name = options.examples_dir + full_object_name + ".cs";
bool is_plain_code = false;
std::string file_name = options.examples_dir + full_object_name + ".xml";
std::ifstream exfile(file_name);
// There is no example file for this class or method, just return
if (!exfile.good()) return true;
if (!exfile.good())
{
// There is no example XML file for this class, try a CS file
file_name = options.examples_dir + full_object_name + ".cs";
exfile.open(file_name);
// There are no example files for this class or method, just return
if (!exfile.good()) return true;
is_plain_code = true;
}
std::stringstream example_buff;
// Start with a newline so the first line renders with same indentation as the rest
example_buff << std::endl << exfile.rdbuf();
if (!as_generator(scope_tab(scope_size) << "/// ").generate(sink, attributes::unused, context)) return false;
if (!generate_opening_tag(sink, "example", context)) return false;
if (!generate_opening_tag(sink, "code", context)) return false;
if (is_plain_code)
{
if (!generate_opening_tag(sink, "example", context)) return false;
if (!generate_opening_tag(sink, "code", context)) return false;
}
if (!generate_escaped_content(sink, example_buff.str(), context)) return false;
if (!generate_closing_tag(sink, "code", context)) return false;
if (!generate_closing_tag(sink, "example", context)) return false;
if (is_plain_code)
{
if (!generate_closing_tag(sink, "code", context)) return false;
if (!generate_closing_tag(sink, "example", context)) return false;
}
return as_generator("\n").generate(sink, attributes::unused, context);
}