Skip to content

Commit

Permalink
WIP configuration s/d dvorka#148.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorka committed Mar 8, 2018
1 parent 608d9ea commit b19a8ce
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 30 deletions.
28 changes: 1 addition & 27 deletions lib/src/config/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include "configuration.h"

#include <iostream>

namespace m8r {

using namespace std;
Expand Down Expand Up @@ -154,31 +152,7 @@ void Configuration::save() const
{
MF_DEBUG("Saving configuration to " << configFilePath);

stringstream s{};
// IMPROVE build more in compile time and less in runtime
s <<
"# MindForger Configuration" << endl <<
endl <<
"This is MindForger configuration file (Markdown hosted DSL)." << endl <<
"See documentation for configuration options details." << endl <<
endl <<
"# Settings" << endl <<
"Application settings:" << endl <<
endl <<
"* Theme: " << uiThemeName << endl <<
" * Examples: dark, light" << endl <<
"* Time scope: " << "0y0m0d0h0m" << endl <<
" * Examples: 2y0m0d0h0m (recent 2 years), 0y3m15d0h0m (recent 3 months and 15 days)" << endl <<
endl <<
"# Repositories" << endl <<
endl <<
"If MindForger detects MindForger repository structure, then the directory is" << endl <<
"threated as knowledge base, else it's used as a Markdown directory (e.g. metadata" << endl <<
"are not stored to Markdown files)." << endl <<
endl <<
"* Active repository: ~/mindforger-repository" << endl <<
"* Repository: ~/mindforger-repository" << endl <<
endl;
migrate to MdConfigRepre

std::ofstream out(configFilePath);
out << s.str();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
markdown_configuration_representation.cpp MindForger thinking notebook
Copyright (C) 2016-2018 Martin Dvorak <[email protected]>
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, see <http://www.gnu.org/licenses/>.
*/
#include "markdown_configuration_representation.h"

namespace m8r {

using namespace std;

MarkdownConfigurationRepresentation::MarkdownConfigurationRepresentation()
{
config = Configuration::getInstance();
}

MarkdownConfigurationRepresentation::~MarkdownConfigurationRepresentation()
{
}

void MarkdownConfigurationRepresentation::configuration(const File& file)
{
Markdown md{&file.name};
md.from();
vector<MarkdownAstNodeSection*>* ast = md.moveAst();

Outline* o = outline(ast);
o->setFormat(md.getFormat());
if(o) {
o->setKey(*md.getFilePath());
o->setBytesize(md.getFileSize());
o->completeProperties(md.getModified());
o->setModifiedPretty(datetimeToPrettyHtml(o->getModified()));
}
return o;
}

// AST to configuration transcoding aims to be as ROBUST as possible to handle
// eventual user typos and incorrect formatting
void MarkdownConfigurationRepresentation::configuration(vector<MarkdownAstNodeSection*>* ast)
{
Outline *outline = new Outline{ontology.getDefaultOutlineType()};
if(ast) {
size_t off = 0;
if(ast->size()) {
MarkdownAstNodeSection* astNode = ast->at(off);

// preamble
if(astNode->isPreambleSection()) {
vector<string*>* body = ast->at(off)->moveBody();
if(body!=nullptr) {
// IMPROVE use body as is
for(string*& bodyItem:*body) {
outline->addPreambleLine(bodyItem);
}
delete body;
}
if(ast->size()>1) {
astNode = ast->at(++off);
} else {
astNode = nullptr;
}
}

if(astNode) {
// O's section
if(astNode->isPostDeclaredSection()) outline->setPostDeclaredSection();
if(astNode->isTrailingHashesSection()) outline->setTrailingHashesSection();
if(astNode->getText()!=nullptr) {
// IMPROVE pull pointer > do NOT copy
outline->setName(*(astNode->getText()));
}
const string* s = astNode->getMetadata().getType();
if(s) {
const OutlineType* outlineType;
// IMPROVE consider string normalization to make parsing more robust
//std::transform(s.begin(), s.end(), s.begin(), ::tolower);
//s[0] = toupper(s[0])
if((outlineType=ontology.getOutlineTypes().get(*s))==nullptr) {
outlineType = ontology.getDefaultOutlineType();
}
outline->setType(outlineType);
}
outline->setCreated(astNode->getMetadata().getCreated());
outline->setModified(astNode->getMetadata().getModified());
outline->setRevision(astNode->getMetadata().getRevision());
outline->setRead(astNode->getMetadata().getRead());
outline->setReads(astNode->getMetadata().getReads());
outline->setImportance(astNode->getMetadata().getImportance());
outline->setUrgency(astNode->getMetadata().getUrgency());
outline->setProgress(astNode->getMetadata().getProgress());

if(astNode->getMetadata().getTags().size()) {
const Tag* t;
for(string* s:astNode->getMetadata().getTags()) {
t = ontology.findOrCreateTag(*s);
outline->addTag(t);
}
}

vector<string*>* body = ast->at(off)->moveBody();
if(body!=nullptr) {
// IMPROVE use body as is
for(string*& bodyItem:*body) {
outline->addDescriptionLine(bodyItem);
}
delete body;
}
}
}

// Ns sections
if(ast->size() > off+1) {
note(ast, off+1, outline);
}

// delete AST
for(MarkdownAstNodeSection* node:*ast) {
if(node!=nullptr) {
delete node;
}
}
delete ast;
}

return outline;
}

string* MarkdownConfigurationRepresentation::to()
{
string* md = new string{};
md->reserve(AVG_OUTLINE_SIZE);
return to(outline, md);
}

string* MarkdownConfigurationRepresentation::to(string* md)
{
stringstream s{};
// IMPROVE build more in compile time and less in runtime
s <<
"# MindForger Configuration" << endl <<
endl <<
"This is MindForger configuration file (Markdown hosted DSL)." << endl <<
"See documentation for configuration options details." << endl <<
endl <<
"# Settings" << endl <<
"Application settings:" << endl <<
endl <<
"* Theme: " << uiThemeName << endl <<
" * Examples: dark, light" << endl <<
"* Time scope: " << "0y0m0d0h0m" << endl <<
" * Examples: 2y0m0d0h0m (recent 2 years), 0y3m15d0h0m (recent 3 months and 15 days)" << endl <<
endl <<
"# Repositories" << endl <<
endl <<
"If MindForger detects MindForger repository structure, then the directory is" << endl <<
"threated as knowledge base, else it's used as a Markdown directory (e.g. metadata" << endl <<
"are not stored to Markdown files)." << endl <<
endl <<
"* Active repository: ~/mindforger-repository" << endl <<
"* Repository: ~/mindforger-repository" << endl <<
endl;
}

} // m8r namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
markdown_configuration_representation.h MindForger thinking notebook
Copyright (C) 2016-2018 Martin Dvorak <[email protected]>
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, see <http://www.gnu.org/licenses/>.
*/
#ifndef M8R_MARKDOWN_CONFIGURATION_REPRESENTATION_H
#define M8R_MARKDOWN_CONFIGURATION_REPRESENTATION_H

#include <string>
#include <vector>
#include <iostream>

#include "markdown.h"
#include "markdown_ast_node.h"
#include "../../config/configuration.h"

namespace m8r {

/**
* @brief Markdown configuration representation.
*/
/* Method:
* Markdown (instance representing MD file)
* FILENAME -lexer-> LINES
* LINES -lexer-> LEXEMS @ LEXER CTX
* LEXEMS -parser-> AST @ PARSER CTX
* set(AST)
*
* MarkdownConfigurationRepresentation (transcoder)
* from(AST) --> OUTLINE
* AST.getString(LEXEM) --> name, description, line, ...
*/
class MarkdownConfigurationRepresentation
{
static constexpr int AVG_SECTION_SIZE = 300;
static constexpr int AVG_CONFIGURATION_SIZE = 2*AVG_NOTE_SIZE;
private:
Configuration config;

public:
explicit MarkdownConfigurationRepresentation();
MarkdownConfigurationRepresentation(const MarkdownConfigurationRepresentation&) = delete;
MarkdownConfigurationRepresentation(const MarkdownConfigurationRepresentation&&) = delete;
MarkdownConfigurationRepresentation &operator=(const MarkdownConfigurationRepresentation&) = delete;
MarkdownConfigurationRepresentation &operator=(const MarkdownConfigurationRepresentation&&) = delete;
~MarkdownConfigurationRepresentation();

virtual void configuration(const File& file);
virtual std::string* to();

private:
void configuration(std::vector<MarkdownAstNodeSection*>* ast);
};

}
#endif // M8R_MARKDOWN_CONFIGURATION_REPRESENTATION_H
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace m8r {

using namespace std;

// IMPROVE endl
const string MarkdownOutlineRepresentation::MINIMAL_SECTION_HEADER = "# T\n";

MarkdownOutlineRepresentation::MarkdownOutlineRepresentation(Ontology& ontology)
Expand Down Expand Up @@ -248,6 +249,7 @@ string* MarkdownOutlineRepresentation::toPreamble(const Outline* outline, string
if(p.size()) {
for(string *s:p) {
md->append(*s);
// IMPROVE endl
md->append("\n");
}
}
Expand Down Expand Up @@ -297,13 +299,15 @@ void MarkdownOutlineRepresentation::toHeader(const Outline* outline, string* md)
if(outline->isPostDeclaredSection()) {
int w=outline->getName().size()<2?2:outline->getName().size();
for(int i=0; i<w; i++) md->append("=");
// IMPROVE endl
md->append("\n");
}

const vector<string*>& description=outline->getDescription();
if(description.size()) {
for(string *s:description) {
md->append(*s);
// IMPROVE endl
md->append("\n");
}
}
Expand Down Expand Up @@ -441,6 +445,7 @@ string* MarkdownOutlineRepresentation::to(const Note* note, string* md, bool inc
for(int i=0; i<w; i++) {
md->append(c);
}
// IMPROVE endl
md->append("\n");
}

Expand All @@ -455,6 +460,7 @@ string* MarkdownOutlineRepresentation::toDescription(const Note* note, string* m
if(description.size()) {
for(string *s:description) {
md->append(*s);
// IMPROVE endl
md->append("\n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace m8r {

/**
* @brief Markdown outline representation.
* @brief Markdown Outline representation.
*/
/* Method:
* Markdown (instance representing MD file)
Expand Down
6 changes: 4 additions & 2 deletions lib/test/mindforger-lib-unit-tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ SOURCES += \
../../lib/test/src/config/configuration_test.cpp \
../../lib/src/mind/ontology/thing_class_rel_triple.cpp \
benchmark/markdown_benchmark.cpp \
../src/mind/aspect/time_scope_aspect.cpp
../src/mind/aspect/time_scope_aspect.cpp \
../src/representations/markdown/markdown_configuration_representation.cpp

HEADERS += \
../../lib/src/debug.h \
Expand Down Expand Up @@ -127,7 +128,8 @@ HEADERS += \
../../lib/src/mind/ontology/thing_class_rel_triple.h \
../../lib/src/mind/ontology/taxonomy.h \
../src/mind/aspect/aspect.h \
../src/mind/aspect/time_scope_aspect.h
../src/mind/aspect/time_scope_aspect.h \
../src/representations/markdown/markdown_configuration_representation.h

# GTest unit test framework library dependencies
unix|win32: LIBS += -lgtest
Expand Down

0 comments on commit b19a8ce

Please sign in to comment.