lumiera_/src/lib/text-template.hpp
Ichthyostega 5881b014fe Library: work out a treatment for text template substitution (see: #1359)
* establish the feature set to provide
 * choose scheme for runtime representation
 * break down analysis to individual parsing and execution steps
 * conclude which actions to conduct and the necessary data
 * derive the abstract binding API required
2024-03-21 19:57:34 +01:00

133 lines
2.9 KiB
C++

/*
TEXT-TEMPLATE.hpp - minimalistic text substitution engine
Copyright (C) Lumiera.org
2024, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file text-template.hpp
** A minimalistic text templating engine with flexible data binding.
**
** @todo WIP-WIP-WIP 3/2024
** @see TextTemplate_test
** @see gnuplot-gen.hpp
** @see SchedulerStress_test
*/
#ifndef LIB_TEXT_TEMPLATE_H
#define LIB_TEXT_TEMPLATE_H
#include "lib/error.hpp"
#include "lib/nocopy.hpp"
#include "lib/iter-explorer.hpp"
#include "lib/format-util.hpp"
//#include "lib/util.hpp"
//#include <cmath>
//#include <limits>
#include <vector>
#include <string>
//#include <stdint.h>
//#include <boost/rational.hpp>
namespace lib {
// using Rat = boost::rational<int64_t>;
// using boost::rational_cast;
// using std::abs;
using std::string;
namespace {// preconfigured TextTemplate data bindings
}
/**
* Text template substitution engine
*/
class TextTemplate
: util::MoveOnly
{
enum Clause {
IF, FOR
};
enum Code {
TEXT, KEY, COND, JUMP, ITER, LOOP
};
struct ParseCtx
{
Clause clause;
};
struct Action
{
Code code;
string val;
};
template<class BIND>
struct Instance
{
};
using PipeTODO = std::vector<string>;
using InstanceIter = decltype (explore (std::declval<PipeTODO const&>()));
public:
TextTemplate(string spec)
{ }
template<class DAT>
InstanceIter
render (DAT const& data) const;
template<class DAT>
static string
apply (string spec, DAT const& data);
};
/** */
template<class DAT>
inline TextTemplate::InstanceIter
TextTemplate::render (DAT const& data) const
{
UNIMPLEMENTED ("actually instantiate the text template");
}
/** */
template<class DAT>
inline string
TextTemplate::apply (string spec, DAT const& data)
{
return util::join (TextTemplate(spec).render (data)
,"");
}
}// namespace lib
#endif /*LIB_TEXT_TEMPLATE_H*/