/* vim: set sw=4 sts=4 et foldmethod=syntax : */ /** \file * * Example \ref example_formatter.cc "example_formatter.cc" . * * \ingroup g_formatters */ /** \example example_formatter.cc * * This example demonstrates how to create a formatter. It outputs information * about a package's dependencies in HTML. * * See \ref example_stringify_formatter.cc "example_stringify_formatter.cc" for * StringifyFormatter, a premade formatter that uses stringify. */ #include #include "example_command_line.hh" #include #include #include using namespace paludis; using namespace examples; using std::cout; using std::endl; namespace { /* Utility function that replaces dodgy characters with HTML escapes. */ std::string escape_html(const std::string & s) { std::string result; for (std::string::const_iterator i(s.begin()), i_end(s.end()) ; i != i_end ; ++i) switch (*i) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; default: result.append(std::string(1, *i)); } return result; } /* Utility function that creates an HTML with a colour. */ std::string span_colour(const std::string & s, const std::string & c) { return "" + s + ""; } /* This formatter outputs information about dependencies in HTML. We need * to implement CanFormat<> for all of the things that can be found in * DependencySpecTree::ItemFormatter, as well as CanSpace. */ class HTMLFormatter : public CanSpace, public CanFormat, public CanFormat, public CanFormat, public CanFormat, public CanFormat { public: /* The second parameter to the format functions has no meaning * beyond being used to overload to the appropriate function. */ std::string format(const PackageDepSpec & s, const format::Plain &) const { return span_colour(escape_html(stringify(s)), "#666666"); } std::string format(const PackageDepSpec & s, const format::Installed &) const { return span_colour(escape_html(stringify(s)), "#6666ff"); } std::string format(const PackageDepSpec & s, const format::Installable &) const { return span_colour(escape_html(stringify(s)), "#66ff66"); } std::string format(const DependenciesLabelsDepSpec & s, const format::Plain &) const { return span_colour(escape_html(stringify(s)), "#666666"); } std::string format(const ConditionalDepSpec & s, const format::Plain &) const { return span_colour(escape_html(stringify(s)), "#666666"); } std::string format(const ConditionalDepSpec & s, const format::Enabled &) const { return span_colour(escape_html(stringify(s)), "#66ff66"); } std::string decorate(const ConditionalDepSpec &, const std::string & s, const format::Added &) const { return s; } std::string decorate(const ConditionalDepSpec &, const std::string & s, const format::Changed &) const { return s; } std::string format(const ConditionalDepSpec & s, const format::Disabled &) const { return span_colour(escape_html(stringify(s)), "#ff6666"); } std::string format(const ConditionalDepSpec & s, const format::Forced &) const { return span_colour(escape_html("(" + stringify(s) + ")"), "#66ff66"); } std::string format(const ConditionalDepSpec & s, const format::Masked &) const { return span_colour(escape_html("(" + stringify(s) + ")"), "#ff6666"); } std::string format(const NamedSetDepSpec & s, const format::Plain &) const { return span_colour(escape_html(stringify(s)), "#666666"); } std::string format(const BlockDepSpec & s, const format::Plain &) const { return span_colour(escape_html(stringify(s)), "#666666"); } std::string newline() const { return "
\n"; } std::string indent(const int i) const { std::string result; for (int x(0) ; x < i ; ++x) result.append("    "); return result; } }; } int main(int argc, char * argv[]) { try { CommandLine::get_instance()->run(argc, argv, "example_formatter", "EXAMPLE_FORMATTER_OPTIONS", "EXAMPLE_FORMATTER_CMDLINE"); /* We start with an Environment, respecting the user's '--environment' choice. */ std::shared_ptr env(EnvironmentFactory::get_instance()->create( CommandLine::get_instance()->a_environment.argument())); /* Fetch package IDs for installable 'sys-apps/paludis'. */ std::shared_ptr ids((*env)[selection::AllVersionsSorted( generator::Package(QualifiedPackageName("sys-apps/paludis")) | filter::SupportsAction())]); /* Write nice valid XHTML, because we're good like that. */ cout << "" << endl; cout << "" << endl; cout << "Dependencies for sys-apps/paludis" << endl; cout << "" << endl; /* For each ID: */ for (PackageIDSequence::ConstIterator i(ids->begin()), i_end(ids->end()) ; i != i_end ; ++i) { cout << "

" << escape_html(stringify(**i)) << "

" << endl; /* Our formatter. It has no saved state, so we can use a single * formatter for all of the keys. */ HTMLFormatter formatter; /* We need to check that _key() methods don't return zero. */ if ((*i)->build_dependencies_key()) { cout << "

" << escape_html((*i)->build_dependencies_key()->human_name()) << "

" << endl; cout << "
" << endl; cout << (*i)->build_dependencies_key()->pretty_print(formatter); cout << endl << "
" << endl; } if ((*i)->run_dependencies_key()) { cout << "

" << escape_html((*i)->run_dependencies_key()->human_name()) << "

" << endl; cout << "
" << endl; cout << (*i)->run_dependencies_key()->pretty_print(formatter); cout << endl << "
" << endl; } if ((*i)->post_dependencies_key()) { cout << "

" << escape_html((*i)->post_dependencies_key()->human_name()) << "

" << endl; cout << "
" << endl; cout << (*i)->post_dependencies_key()->pretty_print(formatter); cout << endl << "
" << endl; } cout << endl; } cout << "" << endl; cout << "" << endl; } catch (const Exception & e) { /* Paludis exceptions can provide a handy human-readable backtrace and * an explanation message. Where possible, these should be displayed. */ cout << endl; cout << "Unhandled exception:" << endl << " * " << e.backtrace("\n * ") << e.message() << " (" << e.what() << ")" << endl; return EXIT_FAILURE; } catch (const std::exception & e) { cout << endl; cout << "Unhandled exception:" << endl << " * " << e.what() << endl; return EXIT_FAILURE; } catch (...) { cout << endl; cout << "Unhandled exception:" << endl << " * Unknown exception type. Ouch..." << endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }