| 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 ApvlvEditor.h |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef _APVLV_NOTE_H_ |
| 29 |
#define _APVLV_NOTE_H_ |
| 30 |
|
| 31 |
#include <map> |
| 32 |
#include <ranges> |
| 33 |
#include <string> |
| 34 |
#include <string_view> |
| 35 |
#include <unordered_set> |
| 36 |
#include <vector> |
| 37 |
|
| 38 |
namespace apvlv |
| 39 |
{ |
| 40 |
constexpr float NoteScoreMin = 0.0f; |
| 41 |
constexpr float NoteScoreMax = 10.0f; |
| 42 |
|
| 43 |
class MarkdownNode; |
| 44 |
struct ApvlvPoint; |
| 45 |
struct Location |
| 46 |
{ |
| 47 |
int page{ 0 }; |
| 48 |
double x{ 0 }; |
| 49 |
double y{ 0 }; |
| 50 |
int offset{ -1 }; |
| 51 |
std::string path; |
| 52 |
std::string anchor; |
| 53 |
|
| 54 |
friend bool |
| 55 |
operator< (const Location &a, const Location &b) |
| 56 |
{ |
| 57 |
if (a.page != b.page) |
| 58 |
return a.page < b.page; |
| 59 |
if (a.y != b.y) |
| 60 |
return a.y < b.y; |
| 61 |
if (a.x != b.x) |
| 62 |
return a.x < b.x; |
| 63 |
if (a.offset != b.offset) |
| 64 |
return a.offset < b.offset; |
| 65 |
if (a.path != b.path) |
| 66 |
return a.path < b.path; |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
void set (int page1, const ApvlvPoint *point1, int offset1 = 0, |
| 71 |
const std::string &path1 = "", const std::string &anchor1 = ""); |
| 72 |
|
| 73 |
void fromMarkdownNode (MarkdownNode *node); |
| 74 |
void toMarkdownNode (MarkdownNode *node) const; |
| 75 |
}; |
| 76 |
|
| 77 |
class File; |
| 78 |
class Note; |
| 79 |
struct Comment |
| 80 |
{ |
| 81 |
Comment () |
| 82 |
: time{ std::chrono::system_clock::to_time_t ( |
| 83 |
std::chrono::system_clock::now ()) } |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
std::string quoteText; |
| 88 |
std::string commentText; |
| 89 |
Location begin; |
| 90 |
Location end; |
| 91 |
time_t time; |
| 92 |
|
| 93 |
void fromMarkdownNode (MarkdownNode *node); |
| 94 |
void toMarkdownNode (MarkdownNode *node) const; |
| 95 |
}; |
| 96 |
|
| 97 |
class Note |
| 98 |
{ |
| 99 |
public: |
| 100 |
explicit Note (File *file); |
| 101 |
~Note (); |
| 102 |
|
| 103 |
bool loadStreamV1 (std::ifstream &is); |
| 104 |
bool loadStream (std::ifstream &is); |
| 105 |
bool load (std::string_view path = ""); |
| 106 |
|
| 107 |
bool dumpStream (std::ostream &os); |
| 108 |
bool dump (std::string_view path = ""); |
| 109 |
|
| 110 |
void |
| 111 |
setScore (float score) |
| 112 |
{ |
| 113 |
mScore = score; |
| 114 |
dump (); |
| 115 |
} |
| 116 |
|
| 117 |
float |
| 118 |
score () |
| 119 |
{ |
| 120 |
return mScore; |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
addTag (const std::string &tag) |
| 125 |
{ |
| 126 |
mTagSet.insert (tag); |
| 127 |
dump (); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
removeTag (const std::string &tag) |
| 132 |
{ |
| 133 |
mTagSet.erase (tag); |
| 134 |
dump (); |
| 135 |
} |
| 136 |
|
| 137 |
const std::unordered_set<std::string> & |
| 138 |
tag () |
| 139 |
{ |
| 140 |
return mTagSet; |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
setRemark (const std::string &remark) |
| 145 |
{ |
| 146 |
mRemark = remark; |
| 147 |
dump (); |
| 148 |
} |
| 149 |
|
| 150 |
const std::string & |
| 151 |
remark () |
| 152 |
{ |
| 153 |
return mRemark; |
| 154 |
} |
| 155 |
|
| 156 |
void |
| 157 |
addReference (const std::string &ref) |
| 158 |
{ |
| 159 |
mReferences.insert (ref); |
| 160 |
dump (); |
| 161 |
} |
| 162 |
|
| 163 |
void |
| 164 |
removeReference (const std::string &ref) |
| 165 |
{ |
| 166 |
mReferences.erase (ref); |
| 167 |
dump (); |
| 168 |
} |
| 169 |
|
| 170 |
const std::unordered_set<std::string> & |
| 171 |
references () |
| 172 |
{ |
| 173 |
return mReferences; |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
addLink (const std::string &link) |
| 178 |
{ |
| 179 |
mLinks.insert (link); |
| 180 |
dump (); |
| 181 |
} |
| 182 |
|
| 183 |
void |
| 184 |
removeLink (const std::string &link) |
| 185 |
{ |
| 186 |
mLinks.erase (link); |
| 187 |
dump (); |
| 188 |
} |
| 189 |
|
| 190 |
const std::unordered_set<std::string> & |
| 191 |
links () |
| 192 |
{ |
| 193 |
return mLinks; |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
addComment (const Comment &comment) |
| 198 |
{ |
| 199 |
mCommentList.insert ({ comment.begin, comment }); |
| 200 |
dump (); |
| 201 |
} |
| 202 |
|
| 203 |
void |
| 204 |
removeComment (const Comment &comment) |
| 205 |
{ |
| 206 |
mCommentList.erase (comment.begin); |
| 207 |
dump (); |
| 208 |
} |
| 209 |
|
| 210 |
std::vector<Comment> |
| 211 |
getCommentsInPage (int page) |
| 212 |
{ |
| 213 |
std::vector<Comment> comments; |
| 214 |
for (const auto &pair1 : mCommentList) |
| 215 |
{ |
| 216 |
if (pair1.first.page == page) |
| 217 |
comments.push_back (pair1.second); |
| 218 |
}; |
| 219 |
return comments; |
| 220 |
} |
| 221 |
|
| 222 |
std::vector<Comment> |
| 223 |
getCommentsInPath (const std::string &path) |
| 224 |
{ |
| 225 |
std::vector<Comment> comments; |
| 226 |
for (const auto &pair1 : mCommentList) |
| 227 |
{ |
| 228 |
if (pair1.first.path == path) |
| 229 |
comments.push_back (pair1.second); |
| 230 |
} |
| 231 |
return comments; |
| 232 |
} |
| 233 |
|
| 234 |
private: |
| 235 |
void loadV1Version (MarkdownNode *node); |
| 236 |
void loadV1MetaData (MarkdownNode *node); |
| 237 |
void loadV1Comments (MarkdownNode *node); |
| 238 |
void loadV1References (MarkdownNode *node); |
| 239 |
void loadV1Links (MarkdownNode *node); |
| 240 |
void appendV1Version (MarkdownNode *doc); |
| 241 |
void appendV1MetaData (MarkdownNode *doc); |
| 242 |
void appendV1Comments (MarkdownNode *doc); |
| 243 |
void appendV1References (MarkdownNode *doc); |
| 244 |
void appendV1Links (MarkdownNode *doc); |
| 245 |
|
| 246 |
std::string notePathOfFile (File *file); |
| 247 |
|
| 248 |
std::string mPath; |
| 249 |
File *mFile{ nullptr }; |
| 250 |
|
| 251 |
float mScore{ 0.0f }; |
| 252 |
|
| 253 |
std::unordered_set<std::string> mTagSet; |
| 254 |
|
| 255 |
std::string mRemark; |
| 256 |
|
| 257 |
std::unordered_set<std::string> mReferences; |
| 258 |
std::unordered_set<std::string> mLinks; |
| 259 |
|
| 260 |
std::map<Location, Comment> mCommentList; |
| 261 |
}; |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
#endif |