NoTeX 1.0.0
A modern noteworthy LaTeX template
Loading...
Searching...
No Matches
notex::Document Class Reference

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.

Detailed Description

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.

Member Typedef Documentation

◆ LinePredicate

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.

Member Function Documentation

◆ find_all()

std::vector< std::size_t > notex::Document::find_all ( const LinePredicate & predicate) const
Returns
The index of every line matching 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().

◆ find_last()

std::optional< std::size_t > notex::Document::find_last ( const LinePredicate & predicate) const
Returns
The index of the last line matching 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().

◆ find_unique()

std::size_t notex::Document::find_unique ( const LinePredicate & predicate,
std::string_view description ) const

Locates the single line matching predicate.

Parameters
predicateTest applied to each line.
descriptionHuman-readable description of what was being looked for, used only to compose the error message when this fails.
Exceptions
DocumentErrorif no line matches, or if more than one does.
Returns
The index of the one matching line.

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().

◆ insert_line()

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().

◆ insert_lines()

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().

◆ lines()

const std::vector< std::string > & notex::Document::lines ( ) const
inlinenoexcept
Returns
Every line of the document, in order, without their trailing newlines.

Definition at line 45 of file document.hpp.

Referenced by load(), and notex::Manager::orphan_sections().

◆ load()

Document notex::Document::load ( const std::filesystem::path & path)
static

Loads path as a sequence of lines.

Exceptions
FilesystemErrorif 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().

◆ remove_line()

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().

◆ save()

void notex::Document::save ( ) const

Writes the current lines back to disk atomically.

Exceptions
FilesystemErrorif 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().

◆ set_documentclass_option()

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.

Parameters
optionOption to make sure is present, e.g. a theme name.
mutually_exclusive_groupThe full set option is drawn from, e.g. every known theme name; every option in this set is removed before option is added.
Exceptions
DocumentErrorif 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().


The documentation for this class was generated from the following files: