Document

The main entry point of the Docraft library. DocraftDocument owns the document tree (DOM), settings, metadata, and drives parsing, templating, and rendering.

class DocraftDocument

High-level document container that owns settings, title, and the DOM node list.

DocraftDocument is the primary API surface for building a document tree, configuring settings, and invoking rendering.

Public Functions

DocraftDocument(std::string document_title = "Untitled Document")

Creates a document with an optional title.

Parameters:

document_title – Human-readable title for the document metadata.

virtual ~DocraftDocument() = default

Virtual destructor.

void add_node(const std::shared_ptr<model::DocraftNode> &node)

Adds a node to the document DOM.

Parameters:

node – Node to append to the document.

void configure_document_settings()

Applies document settings to the underlying rendering context.

void template_document()

Applies template processing to the document DOM using the configured template engine.

void render()

Renders the document using the configured context and renderer.

void set_backend(const std::shared_ptr<backend::IDocraftRenderingBackend> &backend)

Overrides the rendering backend used during render.

Passing nullptr resets to the default backend.

Parameters:

backend – Rendering backend implementation.

void set_document_title(const std::string &document_title)

Sets the document title.

Parameters:

document_title – New title value.

std::string document_title()

Returns the current document title.

Returns:

Document title string.

void set_document_path(const std::string &document_path)

Sets the output directory where the rendered file will be saved.

Parameters:

document_path – Output directory path.

std::string document_path()

Returns the current output directory path.

Returns:

Output directory path.

void set_settings(const std::shared_ptr<model::DocraftSettings> &settings)

Sets document settings (fonts, etc.).

Parameters:

settings – Settings node to apply.

std::shared_ptr<model::DocraftSettings> settings() const

Returns the current settings object.

Returns:

Shared pointer to settings or nullptr if not set.

void set_document_metadata(const DocraftDocumentMetadata &metadata)

Sets document metadata values.

Parameters:

metadata – Metadata values supported by libharu.

const DocraftDocumentMetadata &document_metadata() const

Returns current document metadata values.

Returns:

Metadata object.

void enable_auto_keywords(bool enabled = true)

Enables or disables automatic keyword extraction for metadata.

Parameters:

enabled – true to enable, false to disable.

bool auto_keywords_enabled() const

Returns whether automatic keyword extraction is enabled.

void set_auto_keywords_config(const utils::DocraftKeywordExtractor::Config &config)

Sets configuration for automatic keyword extraction.

Parameters:

config – Extractor configuration.

const utils::DocraftKeywordExtractor::Config &auto_keywords_config() const

Returns the current automatic keyword extraction configuration.

void refresh_auto_keywords()

Extracts keywords from the current document and merges them into metadata.

No-op when auto-keyword extraction is disabled.

void set_document_template_engine(const std::shared_ptr<templating::DocraftTemplateEngine> &template_engine)
std::shared_ptr<templating::DocraftTemplateEngine> document_template_engine() const
const std::vector<std::shared_ptr<model::DocraftNode>> &nodes() const

Returns the document DOM nodes.

Returns:

Vector of root nodes.

std::vector<std::shared_ptr<model::DocraftNode>> get_by_name(const std::string &name) const

Finds nodes by name in the document DOM.

Parameters:

name – Node name to search for.

Returns:

Vector of nodes matching the name, or empty vector if none found.

std::shared_ptr<model::DocraftNode> get_first_by_name(const std::string &name) const

Finds the first node by name in the document DOM.

Parameters:

name – Node name to search for.

Returns:

Shared pointer to the first matching node, or nullptr if not found.

std::shared_ptr<model::DocraftNode> get_last_by_name(const std::string &name) const

Finds the last node by name in the document DOM.

Parameters:

name – Node name to search for.

Returns:

Shared pointer to the last matching node, or nullptr if not found.

template<typename T>
std::vector<std::shared_ptr<T>> get_by_type() const

Finds nodes by type in the document DOM.

Template Parameters:

T – Node type to search for.

Returns:

Vector of nodes matching the type, or empty vector if none found.

void traverse_dom(const std::function<void(const std::shared_ptr<model::DocraftNode>&, DocraftDomTraverseOp)> &callback) const

Traverses the document DOM and executes a callback on each node.

Parameters:

callback – Function called for each node and operation (enter/exit).