Loads a .tex file as a sequence of lines and offers editing primitives anchored on recognisable landmarks. More...
#include <notex/document.hpp>
Public Types | |
| using | LinePredicate = std::function<bool(const std::string&)> |
Public Member Functions | |
| const std::vector< std::string > & | lines () const noexcept |
| std::vector< std::size_t > | find_all (const LinePredicate &predicate) const |
| std::size_t | find_unique (const LinePredicate &predicate, std::string_view description) const |
Locates the single line matching predicate. | |
| std::optional< std::size_t > | find_last (const LinePredicate &predicate) const |
| void | insert_line (std::size_t index, std::string text) |
Inserts text as a new line before the current line index. | |
| void | insert_lines (std::size_t index, std::vector< std::string > new_lines) |
Inserts new_lines, in order, before the current line index. | |
| void | remove_line (std::size_t index) |
Removes the line at index. | |
| void | set_documentclass_option (std::string_view option, const std::vector< std::string > &mutually_exclusive_group) |
Rewrites the document's unique \documentclass line so that its bracketed options contain option instead of whichever member of mutually_exclusive_group was there, preserving every other option untouched. | |
| void | save () const |
| Writes the current lines back to disk atomically. | |
Static Public Member Functions | |
| static Document | load (const std::filesystem::path &path) |
Loads path as a sequence of lines. | |
Loads a .tex file as a sequence of lines and offers editing primitives anchored on recognisable landmarks.
A Document deliberately refuses to guess: locating an anchor throws if it is missing, or if it appears more than once so that the correct location would be ambiguous, rather than picking one silently. This is what makes it safe to run on documents a user has been editing by hand. Changes are only visible on disk once save() is called, which writes atomically (to a temporary file, then renamed over the original) so that an interrupted write can never leave a half-written file behind.
Definition at line 31 of file document.hpp.
| using notex::Document::LinePredicate = std::function<bool(const std::string&)> |
A predicate tested against one line (without its trailing newline) to decide whether it matches an anchor.
Definition at line 35 of file document.hpp.
| std::vector< std::size_t > notex::Document::find_all | ( | const LinePredicate & | predicate | ) | const |
predicate, in order. Definition at line 48 of file document.cpp.
References find_all().
Referenced by notex::Manager::add_bibliography(), find_all(), find_last(), find_unique(), and notex::Manager::remove_section().
| std::optional< std::size_t > notex::Document::find_last | ( | const LinePredicate & | predicate | ) | const |
predicate, or std::nullopt if none does. Unlike find_unique(), more than one match is not an error. Definition at line 80 of file document.cpp.
References find_all(), and find_last().
Referenced by notex::Manager::add_section(), and find_last().
| std::size_t notex::Document::find_unique | ( | const LinePredicate & | predicate, |
| std::string_view | description ) const |
Locates the single line matching predicate.
| predicate | Test applied to each line. |
| description | Human-readable description of what was being looked for, used only to compose the error message when this fails. |
| DocumentError | if no line matches, or if more than one does. |
Definition at line 57 of file document.cpp.
References find_all(), and find_unique().
Referenced by notex::Manager::add_section(), find_unique(), and set_documentclass_option().
| void notex::Document::insert_line | ( | std::size_t | index, |
| std::string | text ) |
Inserts text as a new line before the current line index.
Definition at line 87 of file document.cpp.
References insert_line().
Referenced by notex::Manager::add_section(), and insert_line().
| void notex::Document::insert_lines | ( | std::size_t | index, |
| std::vector< std::string > | new_lines ) |
Inserts new_lines, in order, before the current line index.
Definition at line 92 of file document.cpp.
References insert_lines().
Referenced by notex::Manager::add_section(), and insert_lines().
|
inlinenoexcept |
Definition at line 45 of file document.hpp.
Referenced by load(), and notex::Manager::orphan_sections().
|
static |
Loads path as a sequence of lines.
| FilesystemError | if path cannot be opened for reading. |
Definition at line 35 of file document.cpp.
References lines(), and load().
Referenced by notex::Manager::add_bibliography(), notex::Manager::add_section(), load(), notex::Manager::orphan_sections(), notex::Manager::remove_bibliography(), notex::Manager::remove_section(), and notex::Manager::set_theme().
| void notex::Document::remove_line | ( | std::size_t | index | ) |
Removes the line at index.
Definition at line 99 of file document.cpp.
References remove_line().
Referenced by remove_line(), and notex::Manager::remove_section().
| void notex::Document::save | ( | ) | const |
Writes the current lines back to disk atomically.
| FilesystemError | if the temporary file cannot be written or cannot be renamed over the original. |
Definition at line 169 of file document.cpp.
References save().
Referenced by notex::Manager::add_bibliography(), notex::Manager::add_section(), notex::Manager::remove_bibliography(), notex::Manager::remove_section(), save(), and notex::Manager::set_theme().
| void notex::Document::set_documentclass_option | ( | std::string_view | option, |
| const std::vector< std::string > & | mutually_exclusive_group ) |
Rewrites the document's unique \documentclass line so that its bracketed options contain option instead of whichever member of mutually_exclusive_group was there, preserving every other option untouched.
| option | Option to make sure is present, e.g. a theme name. |
| mutually_exclusive_group | The full set option is drawn from, e.g. every known theme name; every option in this set is removed before option is added. |
| DocumentError | if there is not exactly one \documentclass line, or if it is malformed (missing its {...} class argument). |
Definition at line 103 of file document.cpp.
References find_unique(), and set_documentclass_option().
Referenced by set_documentclass_option(), and notex::Manager::set_theme().