Path: blob/main/contrib/kyua/utils/text/templates.hpp
48178 views
// Copyright 2012 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/text/templates.hpp29/// Custom templating engine for text documents.30///31/// This module provides a simple mechanism to generate text documents based on32/// templates. The templates are just text files that contain template33/// statements that instruct this processor to perform transformations on the34/// input.35///36/// While this was originally written to handle HTML templates, it is actually37/// generic enough to handle any kind of text document, hence why it lives38/// within the utils::text library.39///40/// An example of how the templates look like:41///42/// %if names43/// List of names44/// -------------45/// Amount of names: %%length(names)%%46/// Most preferred name: %%preferred_name%%47/// Full list:48/// %loop names iter49/// * %%last_names(iter)%%, %%names(iter)%%50/// %endloop51/// %endif names5253#if !defined(UTILS_TEXT_TEMPLATES_HPP)54#define UTILS_TEXT_TEMPLATES_HPP5556#include "utils/text/templates_fwd.hpp"5758#include <istream>59#include <map>60#include <ostream>61#include <string>62#include <vector>6364#include "utils/fs/path_fwd.hpp"6566namespace utils {67namespace text {686970/// Definitions of the templates to apply to a file.71///72/// This class provides the environment (e.g. the list of variables) that the73/// templating system has to use when generating the output files. This74/// definition is static in the sense that this is what the caller program75/// specifies.76class templates_def {77/// Mapping of variable names to their values.78typedef std::map< std::string, std::string > variables_map;7980/// Collection of global variables available to the templates.81variables_map _variables;8283/// Convenience name for a vector of strings.84typedef std::vector< std::string > strings_vector;8586/// Mapping of vector names to their contents.87///88/// Ideally, these would be represented as part of the _variables, but we89/// would need a complex mechanism to identify whether a variable is a90/// string or a vector.91typedef std::map< std::string, strings_vector > vectors_map;9293/// Collection of vectors available to the templates.94vectors_map _vectors;9596const std::string& get_vector(const std::string&, const std::string&) const;9798public:99templates_def(void);100101void add_variable(const std::string&, const std::string&);102void remove_variable(const std::string&);103void add_vector(const std::string&);104void add_to_vector(const std::string&, const std::string&);105106bool exists(const std::string&) const;107const std::string& get_variable(const std::string&) const;108const strings_vector& get_vector(const std::string&) const;109110std::string evaluate(const std::string&) const;111};112113114void instantiate(const templates_def&, std::istream&, std::ostream&);115void instantiate(const templates_def&, const fs::path&, const fs::path&);116117118} // namespace text119} // namespace utils120121#endif // !defined(UTILS_TEXT_TEMPLATES_HPP)122123124