| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE ApvlvNote.cc |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <QDir> |
| 29 |
#include <cmark.h> |
| 30 |
#include <filesystem> |
| 31 |
#include <fstream> |
| 32 |
|
| 33 |
#include "ApvlvFile.h" |
| 34 |
#include "ApvlvMarkdown.h" |
| 35 |
#include "ApvlvNote.h" |
| 36 |
#include "ApvlvUtil.h" |
| 37 |
|
| 38 |
namespace apvlv |
| 39 |
{ |
| 40 |
using namespace std; |
| 41 |
|
| 42 |
void |
| 43 |
Location::set (int page1, const ApvlvPoint *point1, int offset1, |
| 44 |
const std::string &path1, const std::string &anchor1) |
| 45 |
{ |
| 46 |
page = page1; |
| 47 |
if (point1) |
| 48 |
{ |
| 49 |
x = point1->x; |
| 50 |
y = point1->y; |
| 51 |
} |
| 52 |
offset = offset1; |
| 53 |
path = path1; |
| 54 |
anchor = anchor1; |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
Location::fromMarkdownNode (MarkdownNode *node) |
| 59 |
{ |
| 60 |
stringstream ss{ node->literal }; |
| 61 |
ss >> page >> x >> y >> offset; |
| 62 |
if (!ss.eof ()) |
| 63 |
{ |
| 64 |
auto str = ss.str (); |
| 65 |
auto pos = str.find ("[["); |
| 66 |
auto end = str.find ("]]"); |
| 67 |
path = str.substr (pos + 2, end - pos - 2); |
| 68 |
anchor = str.substr (end + 2); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void |
| 73 |
Location::toMarkdownNode (MarkdownNode *node) const |
| 74 |
{ |
| 75 |
stringstream ss; |
| 76 |
ss << page << " " << x << " " << y << " " << offset; |
| 77 |
if (!path.empty ()) |
| 78 |
{ |
| 79 |
ss << " [[" << path << "]]#" << anchor; |
| 80 |
} |
| 81 |
node->node_type = CMARK_NODE_TEXT; |
| 82 |
node->literal = ss.str (); |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
Comment::fromMarkdownNode (MarkdownNode *node) |
| 87 |
{ |
| 88 |
auto qn = node->childAt (0); |
| 89 |
auto qp = qn->childAt (0); |
| 90 |
auto qt = qp->childAt (0); |
| 91 |
quoteText = qt->literal; |
| 92 |
|
| 93 |
auto cn = node->childAt (1); |
| 94 |
commentText = cn->literal; |
| 95 |
|
| 96 |
auto list = node->childAt (2); |
| 97 |
|
| 98 |
auto i = list->childAt (0); |
| 99 |
auto p = i->childAt (0); |
| 100 |
auto t = p->childAt (0); |
| 101 |
begin.fromMarkdownNode (t); |
| 102 |
|
| 103 |
i = list->childAt (1); |
| 104 |
p = i->childAt (0); |
| 105 |
t = p->childAt (0); |
| 106 |
end.fromMarkdownNode (t); |
| 107 |
|
| 108 |
i = list->childAt (2); |
| 109 |
p = i->childAt (0); |
| 110 |
t = p->childAt (0); |
| 111 |
struct tm tm{}; |
| 112 |
strptime (t->literal.c_str (), "%a %b %d %H:%M:%S %Y", &tm); |
| 113 |
time = std::mktime (&tm); |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
Comment::toMarkdownNode (MarkdownNode *node) const |
| 118 |
{ |
| 119 |
auto qn = MarkdownNode::create (node, CMARK_NODE_BLOCK_QUOTE); |
| 120 |
auto qp = MarkdownNode::create (qn, CMARK_NODE_PARAGRAPH); |
| 121 |
MarkdownNode::create (qp, CMARK_NODE_TEXT, quoteText); |
| 122 |
|
| 123 |
MarkdownNode::create (node, CMARK_NODE_CODE_BLOCK, commentText); |
| 124 |
|
| 125 |
auto list = MarkdownNode::create (node, CMARK_NODE_LIST); |
| 126 |
list->list_type = CMARK_NO_LIST; |
| 127 |
|
| 128 |
auto i = MarkdownNode::create (list, CMARK_NODE_ITEM); |
| 129 |
auto p = MarkdownNode::create (i, CMARK_NODE_PARAGRAPH); |
| 130 |
auto t = MarkdownNode::create (p, CMARK_NODE_TEXT); |
| 131 |
begin.toMarkdownNode (t); |
| 132 |
|
| 133 |
i = MarkdownNode::create (list, CMARK_NODE_ITEM); |
| 134 |
p = MarkdownNode::create (i, CMARK_NODE_PARAGRAPH); |
| 135 |
t = MarkdownNode::create (p, CMARK_NODE_TEXT); |
| 136 |
end.toMarkdownNode (t); |
| 137 |
|
| 138 |
i = MarkdownNode::create (list, CMARK_NODE_ITEM); |
| 139 |
p = MarkdownNode::create (i, CMARK_NODE_PARAGRAPH); |
| 140 |
t = MarkdownNode::create (p, CMARK_NODE_TEXT); |
| 141 |
t->literal = std::ctime (&time); |
| 142 |
} |
| 143 |
|
| 144 |
Note::Note (File *file) : mFile (file) {} |
| 145 |
|
| 146 |
Note::~Note () = default; |
| 147 |
|
| 148 |
bool |
| 149 |
Note::loadStreamV1 (std::ifstream &is) |
| 150 |
{ |
| 151 |
auto doc = Markdown::create (); |
| 152 |
doc->loadFromStream (is); |
| 153 |
if (!doc) |
| 154 |
{ |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
auto node = doc->root (); |
| 159 |
auto n = node->childAt (0); |
| 160 |
auto list = node->childAt (1); |
| 161 |
loadV1Version (list); |
| 162 |
|
| 163 |
for (auto i = 2; i < node->childrenCount (); i++) |
| 164 |
{ |
| 165 |
n = node->childAt (i); |
| 166 |
if (n->node_type == CMARK_NODE_THEMATIC_BREAK) |
| 167 |
{ |
| 168 |
continue; |
| 169 |
} |
| 170 |
|
| 171 |
if (i >= node->childrenCount () - 1) |
| 172 |
break; |
| 173 |
|
| 174 |
list = node->childAt (i + 1); |
| 175 |
if (list == nullptr) |
| 176 |
break; |
| 177 |
|
| 178 |
// skip empty list of heading |
| 179 |
if (list->node_type == CMARK_NODE_HEADING && list->heading_level == 1) |
| 180 |
{ |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
auto res = n->headText (); |
| 185 |
i++; |
| 186 |
if (res.second == "Meta Data") |
| 187 |
{ |
| 188 |
loadV1MetaData (list); |
| 189 |
} |
| 190 |
else if (res.second == "Comments") |
| 191 |
{ |
| 192 |
loadV1Comments (list); |
| 193 |
} |
| 194 |
else if (res.second == "References") |
| 195 |
{ |
| 196 |
loadV1References (list); |
| 197 |
} |
| 198 |
else if (res.second == "Links") |
| 199 |
{ |
| 200 |
loadV1Links (list); |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
qDebug () << "Unknown head \"" << res.second << "\""; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
bool |
| 212 |
Note::loadStream (std::ifstream &is) |
| 213 |
{ |
| 214 |
string line; |
| 215 |
|
| 216 |
getline (is, line); |
| 217 |
if (!line.starts_with ("---")) |
| 218 |
{ |
| 219 |
qWarning () << "note header not found"; |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
string version; |
| 224 |
is >> version >> version; |
| 225 |
if (version == "1") |
| 226 |
{ |
| 227 |
is.seekg (0, ios::beg); |
| 228 |
return loadStreamV1 (is); |
| 229 |
} |
| 230 |
|
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
bool |
| 235 |
Note::load (std::string_view sv) |
| 236 |
{ |
| 237 |
string path = string (sv); |
| 238 |
if (path.empty ()) |
| 239 |
path = notePathOfFile (mFile); |
| 240 |
|
| 241 |
ifstream ifs{ path }; |
| 242 |
if (!ifs.is_open ()) |
| 243 |
return false; |
| 244 |
|
| 245 |
auto ret = loadStream (ifs); |
| 246 |
ifs.close (); |
| 247 |
return ret; |
| 248 |
} |
| 249 |
|
| 250 |
void |
| 251 |
Note::loadV1Version (MarkdownNode *node) |
| 252 |
{ |
| 253 |
for (auto i = 0; i < node->childrenCount (); i++) |
| 254 |
{ |
| 255 |
auto n = node->childAt (i); |
| 256 |
if (n->node_type == CMARK_NODE_SOFTBREAK) |
| 257 |
continue; |
| 258 |
|
| 259 |
auto s = QString::fromLocal8Bit (n->literal); |
| 260 |
auto vs = s.split (":"); |
| 261 |
if (vs.size () == 2) |
| 262 |
{ |
| 263 |
if (vs[0].trimmed () == "version") |
| 264 |
{ |
| 265 |
auto version = vs[1].trimmed (); |
| 266 |
Q_ASSERT (version == "1"); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void |
| 273 |
Note::loadV1MetaData (MarkdownNode *node) |
| 274 |
{ |
| 275 |
auto texts = node->getListTexts (); |
| 276 |
for (const auto &text : texts) |
| 277 |
{ |
| 278 |
auto tokens = QString::fromLocal8Bit (text).split (":"); |
| 279 |
if (tokens.size () == 2) |
| 280 |
{ |
| 281 |
auto k = tokens[0].trimmed (); |
| 282 |
auto v = tokens[1].trimmed (); |
| 283 |
if (k == "tag") |
| 284 |
{ |
| 285 |
auto ts = v.split (","); |
| 286 |
for (auto const &t : ts) |
| 287 |
{ |
| 288 |
auto tag = t.trimmed (); |
| 289 |
if (!tag.isEmpty ()) |
| 290 |
{ |
| 291 |
qDebug () << "got tag: " << tag; |
| 292 |
mTagSet.insert (tag.toStdString ()); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
else if (k == "score") |
| 297 |
{ |
| 298 |
mScore = v.toFloat (); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
void |
| 305 |
Note::loadV1Comments (MarkdownNode *node) |
| 306 |
{ |
| 307 |
for (auto i = 0; i < node->childrenCount (); i++) |
| 308 |
{ |
| 309 |
auto ni = node->childAt (i); |
| 310 |
auto comment = Comment{}; |
| 311 |
comment.fromMarkdownNode (ni); |
| 312 |
addComment (comment); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
void |
| 317 |
Note::loadV1References (MarkdownNode *node) |
| 318 |
{ |
| 319 |
auto texts = node->getListTexts (); |
| 320 |
for (const auto &text : texts) |
| 321 |
{ |
| 322 |
if (!text.empty ()) |
| 323 |
{ |
| 324 |
mReferences.insert (text); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
void |
| 330 |
Note::loadV1Links (MarkdownNode *node) |
| 331 |
{ |
| 332 |
auto texts = node->getListTexts (); |
| 333 |
for (const auto &text : texts) |
| 334 |
{ |
| 335 |
if (!text.empty ()) |
| 336 |
{ |
| 337 |
mLinks.insert (text); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void |
| 343 |
Note::appendV1Version (MarkdownNode *doc) |
| 344 |
{ |
| 345 |
MarkdownNode::create (doc, CMARK_NODE_THEMATIC_BREAK); |
| 346 |
|
| 347 |
auto g = MarkdownNode::create (doc, CMARK_NODE_PARAGRAPH); |
| 348 |
MarkdownNode::create (g, CMARK_NODE_TEXT, "version: 1"); |
| 349 |
MarkdownNode::create (g, CMARK_NODE_LINEBREAK); |
| 350 |
MarkdownNode::create (g, CMARK_NODE_TEXT, "path: " + mPath); |
| 351 |
|
| 352 |
MarkdownNode::create (doc, CMARK_NODE_THEMATIC_BREAK); |
| 353 |
} |
| 354 |
|
| 355 |
void |
| 356 |
Note::appendV1MetaData (MarkdownNode *doc) |
| 357 |
{ |
| 358 |
MarkdownNode::HeadAndList headAndList; |
| 359 |
headAndList.first = "Meta Data"; |
| 360 |
string tags; |
| 361 |
for (const auto &cp : mTagSet) |
| 362 |
{ |
| 363 |
tags += cp + ","; |
| 364 |
} |
| 365 |
headAndList.second.push_back ("score: " |
| 366 |
+ QString::number (mScore).toStdString ()); |
| 367 |
headAndList.second.push_back ("tag: " + tags); |
| 368 |
doc->appendHeadAndBulletList (1, headAndList); |
| 369 |
} |
| 370 |
|
| 371 |
void |
| 372 |
Note::appendV1Comments (MarkdownNode *doc) |
| 373 |
{ |
| 374 |
doc->appendHeadText (1, "Comments"); |
| 375 |
|
| 376 |
auto list = MarkdownNode::create (doc, CMARK_NODE_LIST); |
| 377 |
list->list_type = CMARK_ORDERED_LIST; |
| 378 |
for (auto const &loc_comment : mCommentList) |
| 379 |
{ |
| 380 |
auto comment = loc_comment.second; |
| 381 |
auto n = MarkdownNode::create (list, CMARK_NODE_ITEM); |
| 382 |
comment.toMarkdownNode (n); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void |
| 387 |
Note::appendV1References (MarkdownNode *doc) |
| 388 |
{ |
| 389 |
MarkdownNode::HeadAndList headAndList; |
| 390 |
headAndList.first = "References"; |
| 391 |
for (auto const &r : mReferences) |
| 392 |
{ |
| 393 |
headAndList.second.push_back (r); |
| 394 |
} |
| 395 |
doc->appendHeadAndBulletList (1, headAndList); |
| 396 |
} |
| 397 |
|
| 398 |
void |
| 399 |
Note::appendV1Links (MarkdownNode *doc) |
| 400 |
{ |
| 401 |
MarkdownNode::HeadAndList headAndList; |
| 402 |
headAndList.first = "Links"; |
| 403 |
for (auto const &r : mLinks) |
| 404 |
{ |
| 405 |
headAndList.second.push_back (r); |
| 406 |
} |
| 407 |
doc->appendHeadAndBulletList (1, headAndList); |
| 408 |
} |
| 409 |
|
| 410 |
bool |
| 411 |
Note::dumpStream (std::ostream &os) |
| 412 |
{ |
| 413 |
/* handmade version is ugly */ |
| 414 |
#if 0 |
| 415 |
os << "---" << endl; |
| 416 |
os << "version: 1" << endl; |
| 417 |
os << "path: " << mFile->getFilename () << endl; |
| 418 |
|
| 419 |
os << "---" << endl; |
| 420 |
os << "# Meta Data" << endl; |
| 421 |
os << "- tag: "; |
| 422 |
std::ranges::for_each (mTagSet, |
| 423 |
[&os] (const string &tag) { os << tag << ","; }); |
| 424 |
os << endl; |
| 425 |
os << "- score: " << mScore << endl; |
| 426 |
os << endl; |
| 427 |
|
| 428 |
os << "# Comments" << endl; |
| 429 |
auto index = 0; |
| 430 |
std::ranges::for_each (mCommentList, |
| 431 |
[&os, &index] (const pair<Location, Comment> &pair1) { |
| 432 |
os << " - " << index++ << endl; |
| 433 |
os << pair1.second; |
| 434 |
}); |
| 435 |
os << endl; |
| 436 |
|
| 437 |
os << "# References" << endl; |
| 438 |
std::ranges::for_each ( |
| 439 |
mReferences, [&os] (const string &ref) { os << "- " << ref << endl; }); |
| 440 |
os << endl; |
| 441 |
|
| 442 |
os << "# Links" << endl; |
| 443 |
std::ranges::for_each ( |
| 444 |
mLinks, [&os] (const string &link) { os << "- " << link << endl; }); |
| 445 |
os << endl; |
| 446 |
|
| 447 |
#else |
| 448 |
auto doc = Markdown::create (); |
| 449 |
auto node = doc->root (); |
| 450 |
|
| 451 |
appendV1Version (node); |
| 452 |
|
| 453 |
appendV1MetaData (node); |
| 454 |
|
| 455 |
appendV1Comments (node); |
| 456 |
|
| 457 |
appendV1References (node); |
| 458 |
|
| 459 |
appendV1Links (node); |
| 460 |
|
| 461 |
doc->saveToStream (os); |
| 462 |
#endif |
| 463 |
|
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
bool |
| 468 |
Note::dump (std::string_view sv) |
| 469 |
{ |
| 470 |
if (mFile == nullptr) |
| 471 |
return false; |
| 472 |
|
| 473 |
string path = string (sv); |
| 474 |
if (path.empty ()) |
| 475 |
path = notePathOfFile (mFile); |
| 476 |
|
| 477 |
auto fspath = filesystem::path (path).parent_path (); |
| 478 |
std::error_code code; |
| 479 |
filesystem::create_directories (fspath, code); |
| 480 |
|
| 481 |
ofstream ofs{ path }; |
| 482 |
if (!ofs.is_open ()) |
| 483 |
return false; |
| 484 |
|
| 485 |
auto ret = dumpStream (ofs); |
| 486 |
ofs.close (); |
| 487 |
return ret; |
| 488 |
} |
| 489 |
|
| 490 |
std::string |
| 491 |
Note::notePathOfFile (File *file) |
| 492 |
{ |
| 493 |
auto homedir = QDir::home ().filesystemAbsolutePath ().string (); |
| 494 |
auto filename = file->getFilename (); |
| 495 |
if (filename.find (homedir) == 0) |
| 496 |
filename = filename.substr (homedir.size () + 1); |
| 497 |
if (filename[0] == filesystem::path::preferred_separator) |
| 498 |
filename = filename.substr (1); |
| 499 |
if (filename[1] == ':') |
| 500 |
filename[1] = '-'; |
| 501 |
auto path = NotesDir + filesystem::path::preferred_separator + filename; |
| 502 |
return path + ".md"; |
| 503 |
} |
| 504 |
} |