API

gresiblos - greyrat's simple blog system.

Template

A class realising a template with optional fields and fields to replace by given values

Attributes:
  • template (str) –

    The string template to be used by the Template object.

__init__(template)

Initialize a Template object with a given string template.

Parameters:
  • template (str) –

    The string template to be used by the Template object.

process_optional_fields(tpl, values)

Remove optional fields from the template string. Optional fields are enclosed in [[[:?key:?]] ... [[key?:]].

Parameters:
  • tpl (str) –

    The template string containing optional fields.

  • values (dict) –

    A dictionary containing the values for the optional fields.

Returns:
  • str( str ) –

    The modified template string with optional fields removed or replaced by their corresponding values.

Raises:
  • SystemExit

    If a missing closing tag is detected, the function will exit with an error code 3.

encode_topics(values, topics_format)

Encodes the 'topics' field from a dictionary into HTML format based on a given format string.

Parameters:
  • values (dict) –

    The input dictionary containing the 'topics' field.

  • topics_format (str) –

    A format string that includes '[[:topic:]]' as a placeholder for each topic.

Returns:
  • str( str ) –

    A comma-separated string of HTML-encoded topics.

embed(values, topics_format='[[:topic:]]')

Embeds the given values into a template.

Parameters:
  • values (Dict[str, str]) –

    The values to embed.

  • topics_format (str, default: '[[:topic:]]' ) –

    The format of the topics.

Entry

Represents a blog entry with metadata and content.

Attributes:
  • _fields (Dict[str, str]) –

    A dictionary to store entry fields.

  • _date (datetime) –

    The date the entry was written at.

  • _destination (str) –

    The path and complete filename the entry shall be written to.

__init__(fields=None)

Initializes an Entry object with default values.

Parameters:
  • fields (Dict[str, str], default: None ) –

    The entry's meta data and content.

get_date()

Returns the date the entry was written at.

Returns:
  • datetime

    The date the entry was written at.

get_destination()

Returns the path and complete filename the entry shall be written to.

Returns:
  • str

    The path and complete filename the entry shall be written to.

get(key)

Returns the value of a field by key.

Parameters:
  • key (str) –

    The key of the field to retrieve.

Returns:
  • str

    The value of the field.

has_key(key)

Returns whether the key is known.

Parameters:
  • key (str) –

    The key of the field to check for.

Returns:
  • bool

    Whether the named field is stored.

_consolidate(filename, date_format, extension)

Consolidates the entry fields from a file.

Parameters:
  • filename (str) –

    The path to the file.

  • date_format (str) –

    The format of the date in the file. If None, it will be parsed as ISO 8601.

  • extension (str) –

    The file extension.

load(filename, date_format, extension)

Loads entry data from a filename.

Parameters:
  • filename (str) –

    The path to the filename containing entry data.

  • date_format (str) –

    The date format used in the file.

  • extension (str) –

    The extension the result shall have.

apply_processors(apply_markdown, prettifier, to_html)

Applies text processors optionally: a) converts markdown to HTML b) applies degrotesque

Parameters:
  • apply_markdown (bool) –

    If set, markdown is applied.

  • prettifier (Any) –

    If given, the prettifier is applied.

  • to_html (bool) –

    Whether basic HTML tags shall be added

PlainStorage

Stores blog entries.

Attributes:
  • _entries (Dict[str, Entry]) –

    A dictionary to store entries by filename.

__init__()

Initialize a new instance of PlainStorage.

This method initializes an empty dictionary to store entries.

add(entry)

Adds an entry's metadata to the storage.

Parameters:
  • entry (Entry) –

    The Entry object containing metadata.

Returns:
  • bool

    False if an entry with same output name already exists.

get_entries()

Returns all stored entries' metadata as a list.

Returns:
  • List[Entry]

    The list of entries.

build_json_index(index_indent)

Returns all stored entries' metadata as a list.

Parameters:
  • index_indent (int) –

    The indentation level to use for JSON encoding

Returns:
  • str

    Metadata of all entries in JSON format.

load_template(path, filename)

Loads a template either from a given path or from the data folder.

Parameters:
  • path (str) –

    The title to apply.

  • filename (str) –

    The filename of the entry.

Returns:
  • Template( Template ) –

    The loaded template.

write_list(title, dest_path, template, entries, topic_format='[[:topic:]]')

Generates an unordered list from the given list of entry metadata, embeds it into the given template, and saves the result under the given path.

Parameters:
  • title (str) –

    The title to apply.

  • dest_path (str) –

    The filename of the entry.

  • template (Template) –

    The template to fill.

  • entries (List[Entry]) –

    A list of entry metadata.

  • topic_format (str, default: '[[:topic:]]' ) –

    The format of topics to use.

write_feed(storage, feed_type, args, dest_path)

Generates a simple RSS 2.0 or Atom feed listing the entries stored in the given storage.

Parameters:
  • storage (PlainStorage) –

    The storage containing all entries.

  • feed_type (str) –

    The type of the feed ('rss' / 'atom').

  • args (Namespace) –

    Additional arguments required for generating the feed.

  • dest_path (str) –

    The path to write the RSS feed to.

get_args(arguments=None)

Parse command line arguments.

Parameters:
  • arguments (List[str], default: None ) –

    Command line arguments to parse.

Returns:
  • Namespace

    argparse.Namespace: Parsed command line arguments.

collect_files_sorted(input_arg)

Collects the files defined by the given input definition.

Parameters:
  • input_arg (str) –

    The definition of the files to load.

Returns:
  • List[str]

    The list of collected file names.

main(arguments=None)

The main method using parameters from the command line.

Parameters:
  • arguments (List[str], default: None ) –

    A list of command line arguments.

Returns:
  • int

    The exit code (0 for success).

script_run()

Execute from command line.