[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master fb01571 035/156: Pass what is being expanded
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master fb01571 035/156: Pass what is being expanded to interpolation function |
Date: |
Tue, 30 Jan 2018 17:22:04 -0500 (EST) |
branch: master
commit fb01571cd03d4bd03869ec69337a91da945019b6
Author: Vadim Zeitlin <address@hidden>
Commit: Vadim Zeitlin <address@hidden>
Pass what is being expanded to interpolation function
Add an extra parameter to the lookup function used by
interpolate_string() to make it possible distinguish variables from
sections and from other things in the future.
No real changes yet.
---
interpolate_string.cpp | 12 +++++++++---
interpolate_string.hpp | 11 ++++++++++-
interpolate_string_test.cpp | 33 +++++++++++++++++++++++++++++----
ledger_pdf_generator_wx.cpp | 5 ++++-
4 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/interpolate_string.cpp b/interpolate_string.cpp
index 0269f78..db3b9d3 100644
--- a/interpolate_string.cpp
+++ b/interpolate_string.cpp
@@ -31,7 +31,7 @@
std::string interpolate_string
(char const* s
- ,std::function<std::string (std::string const&)> const& lookup
+ ,lookup_function const& lookup
)
{
std::string out;
@@ -107,7 +107,10 @@ std::string interpolate_string
bool active = is_active();
if(active)
{
- auto const value = lookup(real_name);
+ auto const value = lookup
+ (real_name
+ ,interpolate_lookup_kind::section
+ );
if(value == "1")
{
active = true;
@@ -176,7 +179,10 @@ std::string interpolate_string
// variable name may seem strange, but why not
// allow using "{{}}" to insert something into
// the interpolated string, after all?
- out += lookup(name);
+ out += lookup
+ (name
+ ,interpolate_lookup_kind::variable
+ );
}
}
diff --git a/interpolate_string.hpp b/interpolate_string.hpp
index e935a7a..ef2de4d 100644
--- a/interpolate_string.hpp
+++ b/interpolate_string.hpp
@@ -27,6 +27,15 @@
#include <functional>
#include <string>
+enum class interpolate_lookup_kind
+{
+ variable,
+ section
+};
+
+using lookup_function
+ = std::function<std::string (std::string const&, interpolate_lookup_kind)>;
+
/// Interpolate string containing embedded variable references.
///
/// Return the input string after replacing all {{variable}} references in it
@@ -50,7 +59,7 @@
/// Throw if the lookup function throws or if the string uses invalid syntax.
std::string interpolate_string
(char const* s
- ,std::function<std::string (std::string const&)> const& lookup
+ ,lookup_function const& lookup
);
#endif // interpolate_string_hpp
diff --git a/interpolate_string_test.cpp b/interpolate_string_test.cpp
index e12d931..459a297 100644
--- a/interpolate_string_test.cpp
+++ b/interpolate_string_test.cpp
@@ -29,7 +29,10 @@ int test_main(int, char*[])
{
auto const test_interpolate = [](char const* s)
{
- return interpolate_string(s, [](std::string const& k) { return k; });
+ return interpolate_string
+ (s
+ ,[](std::string const& k, interpolate_lookup_kind) { return k; }
+ );
};
// Check that basic interpolation works.
@@ -46,7 +49,7 @@ int test_main(int, char*[])
{
return interpolate_string
(s
- ,[](std::string const& s) -> std::string
+ ,[](std::string const& s, interpolate_lookup_kind) -> std::string
{
if(s == "var0") return "0";
if(s == "var1") return "1";
@@ -83,7 +86,7 @@ int test_main(int, char*[])
BOOST_TEST_EQUAL
(interpolate_string
("{{expanded}}"
- ,[](std::string const& s) -> std::string
+ ,[](std::string const& s, interpolate_lookup_kind) -> std::string
{
if(s == "expanded")
{
@@ -95,6 +98,28 @@ int test_main(int, char*[])
,"{{unexpanded}}"
);
+ // Check that the kind of variable being expanded is correct.
+ BOOST_TEST_EQUAL
+ (interpolate_string
+ ("{{#section1}}{{^section0}}{{variable}}{{/section0}}{{/section1}}"
+ ,[](std::string const& s, interpolate_lookup_kind kind)
+ {
+ switch(kind)
+ {
+ case interpolate_lookup_kind::variable:
+ return "value of " + s;
+
+ case interpolate_lookup_kind::section:
+ // Get rid of the "section" prefix.
+ return s.substr(7);
+ }
+
+ throw std::runtime_error("invalid lookup kind");
+ }
+ )
+ ,"value of variable"
+ );
+
// Should throw if the input syntax is invalid.
BOOST_TEST_THROW
(test_interpolate("{{x")
@@ -131,7 +156,7 @@ int test_main(int, char*[])
BOOST_TEST_THROW
(interpolate_string
("{{x}}"
- ,[](std::string const& s) -> std::string
+ ,[](std::string const& s, interpolate_lookup_kind) -> std::string
{
throw std::runtime_error("no such variable '" + s + "'");
}
diff --git a/ledger_pdf_generator_wx.cpp b/ledger_pdf_generator_wx.cpp
index bb7987f..d258771 100644
--- a/ledger_pdf_generator_wx.cpp
+++ b/ledger_pdf_generator_wx.cpp
@@ -91,7 +91,10 @@ class html_interpolator
return text::from_html
(interpolate_string
(s
- ,[this](std::string const& s) { return
expand_html(s).as_html(); }
+ ,[this](std::string const& s, interpolate_lookup_kind)
+ {
+ return expand_html(s).as_html();
+ }
)
);
}
- [lmi-commits] [lmi] master 95e616e 105/156: Add wx_table_generator::columns_count() accessor, (continued)
- [lmi-commits] [lmi] master 95e616e 105/156: Add wx_table_generator::columns_count() accessor, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 3f1d437 088/156: Add add_abbreviated_variable() helper, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 118a249 097/156: Add "Column Definitions and Key Terms" page to NASD illustrations, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 1ab47c0 063/156: Fix definition of integer-valued variable in PDF illustrations, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master bb21f4d 077/156: Fix header for composite illustrations, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master d2b2cae 038/156: Change column_headings page to use external template, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 2bb1c91 041/156: Remove unnecessary outer html and body tags, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 1afd45c 040/156: Avoid spurious coding style violation, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 9695e9e 112/156: Create InitAnnLoanDueRate vector in addition to the scalar, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 5f6dc89 131/156: Use string objects for illustration_table_column fields, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master fb01571 035/156: Pass what is being expanded to interpolation function,
Greg Chicares <=
- [lmi-commits] [lmi] master fc6d8b7 034/156: Implement the "Columns Headings" page, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 46788b8 119/156: Start implementing individual private placement illustration kind, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 1287474 114/156: Start private group placement illustration class implementation, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 0f78daf 050/156: Compute the footer size in page_with_footer::pre_render(), Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 6b3b5b2 054/156: Factor out illustration_table_generator from tabular_detail2_page, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 23d26f8 055/156: Factor out reusable page_with_tabular_report class, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master f484622 044/156: Only show numeric summary if not in force, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 67db94b 042/156: Implement more of numeric summary page, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master dbaf3c4 048/156: Render PDF illustrations in two passes, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master de9567e 152/156: Fix misplaced commas in ledger PDF generator code, Greg Chicares, 2018/01/30