Commit 8188d143 authored by aggsol's avatar aggsol 🙉

extacted writer code

parent bcffd738
......@@ -6,7 +6,7 @@ Helper tool for gamebooks written in Asciidoc
## Todo
1. Shuffle of sections, verify shuffle
1. Enforce `+` at the end of section lines?
2. Better test cases
## Build
......@@ -105,5 +105,5 @@ Tags of sections helps to debug and naviagte the story quickliy with the generat
## Licenses
* gamebook-checker is GPLv3
* [rang](https://github.com/agauniyal/rang) is Public Domaine
* [rang](https://github.com/agauniyal/rang) is Public Domain
* tiny-unit is Beerware
\ No newline at end of file
......@@ -3,6 +3,7 @@ add_library(
STATIC
Graph.cpp
Parser.cpp
Writer.cpp
)
target_include_directories(
......
......@@ -20,6 +20,7 @@
#include "Helpers.hpp"
#include "Section.hpp"
#include "rang.hpp"
#include <cassert>
#include <iostream>
......@@ -51,7 +52,7 @@ namespace bodhi
const int minDelta = std::max<int>(3, numbers.size()/10);
const int maxAttempts = std::max<int>(10, numbers.size()/4);
std::cout << "Shuffle: minDelta=" << minDelta << " maxAttempts=" << maxAttempts << "\n";
std::cerr << "Shuffle: minDelta=" << minDelta << " maxAttempts=" << maxAttempts << "\n";
while(numbers.size() > 1)
{
......@@ -63,7 +64,7 @@ namespace bodhi
int b = numbers.at(index);
int counter = 0;
while( (abs(a-b) < minDelta) && (counter < 10))
while( (abs(a-b) < minDelta) && (counter < maxAttempts))
{
index = bodhi::randint(generator, 0, numbers.size()-1);
b = numbers.at(index);
......@@ -71,7 +72,9 @@ namespace bodhi
}
if(counter >= maxAttempts)
{
std::cerr << "Warning: Could not map section " << a << " farther away\n";
std::cerr << rang::fg::yellow
<< "Warning: Could not map section " << a << " farther away\n"
<< rang::fg::reset;
}
numbers.erase(numbers.begin() + index);
......
/*
Copyright (C) 2018 Kim HOANG <foss@aggsol.de>
This file is part of gamebook-checker.
xollox 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "Writer.hpp"
#include "Helpers.hpp"
#include <cassert>
namespace bodhi
{
Writer::Writer(bool verbose)
: m_verbose(verbose)
{
}
void Writer::writeSections(std::map<int, bodhi::Section>& sections,
const std::map<int, int> resultMapping,
std::ostream& strm)
{
std::map<int, bodhi::Section> final;
for(auto& s: sections)
{
assert(s.first == s.second.m_number);
const int shuffled = resultMapping.at(s.first);
if(m_verbose)
{
std::cerr << s.first << " -> " << shuffled << "\n";
}
s.second.m_number = shuffled;
// First pass substitution
std::map<std::string, std::string> placeholders;
for(auto& c: s.second.m_children)
{
std::string before = "<<section-";
before += std::to_string(c);
before += ">>";
std::string placeholder = "<<temp-";
placeholder += std::to_string(c);
placeholder += "-";
placeholder += std::to_string(resultMapping.at(c));
placeholder += ">>";
std::string after = "<<section-";
after += std::to_string(resultMapping.at(c));
after += ">>";
placeholders[placeholder] = after;
if(m_verbose)
{
std::cerr << "before='" << before << "'\t "
<< "temp ='" << placeholder << "'\t "
<< "after ='" << after << "'\n";
}
for(auto& l: s.second.m_lines)
{
l = bodhi::replaceSubStr(l, before, placeholder);
}
}
// Second pass substitution
for(auto& l: s.second.m_lines)
{
for(auto& p: placeholders)
{
l = bodhi::replaceSubStr(l, p.first, p.second);
}
}
final[shuffled] = s.second;
}
for(auto& f: final)
{
assert(f.first == f.second.m_number);
strm << "[[section-" << f.second.m_number << "]]\n"
<< "=== " << f.second.m_number << "\n";
for(auto& l: f.second.m_lines)
{
strm << l << "\n";
}
}
}
}
\ No newline at end of file
/*
Copyright (C) 2018 Kim HOANG <foss@aggsol.de>
This file is part of gamebook-checker.
xollox 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "Section.hpp"
#include <map>
#include <iostream>
namespace bodhi
{
class Writer
{
public:
explicit Writer(bool verbose);
void writeSections(std::map<int, bodhi::Section>& sections,
const std::map<int, int> resultMapping,
std::ostream& strm);
private:
bool m_verbose;
};
}
\ No newline at end of file
......@@ -17,7 +17,7 @@
*/
#include "CliArguments.hpp"
#include "Graph.hpp"
#include "Helpers.hpp"
#include "Writer.hpp"
#include "Parser.hpp"
#include "Shuffle.hpp"
#include "rang.hpp"
......@@ -57,80 +57,6 @@ namespace
<< std::endl;
}
void writeSections(std::map<int, bodhi::Section>& sections,
const std::map<int, int> resultMapping,
std::ostream& strm)
{
std::map<int, bodhi::Section> final;
for(auto& s: sections)
{
assert(s.first == s.second.m_number);
const int oldSection = s.first;
const int shuffled = resultMapping.at(s.first);
std::cerr << s.first << " -> " << shuffled << "\n";
s.second.m_number = shuffled;
// First pass substitution
std::map<std::string, std::string> placeholders;
for(auto& c: s.second.m_children)
{
std::string before = "<<section-";
before += std::to_string(c);
before += ">>";
std::string placeholder = "<<temp-";
placeholder += std::to_string(c);
placeholder += "-";
placeholder += std::to_string(resultMapping.at(c));
placeholder += ">>";
std::string after = "<<section-";
after += std::to_string(resultMapping.at(c));
after += ">>";
placeholders[placeholder] = after;
if(oldSection == 22)
{
std::cerr << "before='" << before << "'\t "
<< "temp ='" << placeholder << "'\t "
<< "after ='" << after << "'\n";
}
for(auto& l: s.second.m_lines)
{
l = bodhi::replaceSubStr(l, before, placeholder);
}
}
// Second pass substitution
for(auto& l: s.second.m_lines)
{
for(auto& p: placeholders)
{
l = bodhi::replaceSubStr(l, p.first, p.second);
}
}
final[shuffled] = s.second;
}
for(auto& f: final)
{
assert(f.first == f.second.m_number);
strm << "[[section-" << f.second.m_number << "]]\n"
<< "=== " << f.second.m_number << "\n";
for(auto& l: f.second.m_lines)
{
strm << l << "\n";
}
}
}
}
int main(int argc, char* argv[])
......@@ -256,11 +182,13 @@ int main(int argc, char* argv[])
if(mix)
{
bodhi::Writer writer(verbose);
auto resultMapping = bodhi::createSectionMapping(sections, seed);
if(output == "stdout")
{
writeSections(sections, resultMapping, std::cout);
writer.writeSections(sections, resultMapping, std::cout);
}
else
{
......@@ -269,7 +197,7 @@ int main(int argc, char* argv[])
{
throw std::runtime_error("Cannot open output file.");
}
writeSections(sections, resultMapping, mixFile);
writer.writeSections(sections, resultMapping, mixFile);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment