API
gresiblos - greyrat's simple blog system.
Template
A class realising a template with optional fields and fields to replace by given values
Attributes:
| Name | Type | Description |
|---|---|---|
template |
str
|
The string template to be used by the Template object. |
__init__(template)
Initialize a Template object with a given string template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
The string template to be used by the Template object. |
required |
process_optional_fields(tpl, values)
Remove optional fields from the template string. Optional fields are enclosed in [[[:?key:?]] ... [[key?:]].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tpl
|
str
|
The template string containing optional fields. |
required |
values
|
dict
|
A dictionary containing the values for the optional fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The modified template string with optional fields removed or replaced by their corresponding values. |
Raises:
| Type | Description |
|---|---|
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:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
dict
|
The input dictionary containing the 'topics' field. |
required |
topics_format
|
str
|
A format string that includes '[[:topic:]]' as a placeholder for each topic. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A comma-separated string of HTML-encoded topics. |
embed(values, topics_format='[[:topic:]]')
Embeds the given values into a template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Dict[str, str]
|
The values to embed. |
required |
topics_format
|
str
|
The format of the topics. |
'[[:topic:]]'
|
Entry
Represents a blog entry with metadata and content.
Attributes:
| Name | Type | Description |
|---|---|---|
_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:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
Dict[str, str]
|
The entry's meta data and content. |
None
|
get_date()
Returns the date the entry was written at.
Returns:
| Type | Description |
|---|---|
datetime
|
The date the entry was written at. |
get_destination()
Returns the path and complete filename the entry shall be written to.
Returns:
| Type | Description |
|---|---|
str
|
The path and complete filename the entry shall be written to. |
get(key)
Returns the value of a field by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key of the field to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The value of the field. |
has_key(key)
Returns whether the key is known.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key of the field to check for. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the named field is stored. |
_consolidate(filename, date_format, extension)
Consolidates the entry fields from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the file. |
required |
date_format
|
str
|
The format of the date in the file. If None, it will be parsed as ISO 8601. |
required |
extension
|
str
|
The file extension. |
required |
load(filename, date_format, extension)
Loads entry data from a filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the filename containing entry data. |
required |
date_format
|
str
|
The date format used in the file. |
required |
extension
|
str
|
The extension the result shall have. |
required |
apply_processors(apply_markdown, prettifier, to_html)
Applies text processors optionally: a) converts markdown to HTML b) applies degrotesque
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
apply_markdown
|
bool
|
If set, markdown is applied. |
required |
prettifier
|
Any
|
If given, the prettifier is applied. |
required |
to_html
|
bool
|
Whether basic HTML tags shall be added |
required |
PlainStorage
Stores blog entries.
Attributes:
| Name | Type | Description |
|---|---|---|
_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:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
Entry
|
The Entry object containing metadata. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
False if an entry with same output name already exists. |
get_entries()
Returns all stored entries' metadata as a list.
Returns:
| Type | Description |
|---|---|
List[Entry]
|
The list of entries. |
build_json_index(index_indent)
Returns all stored entries' metadata as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_indent
|
int
|
The indentation level to use for JSON encoding |
required |
Returns:
| Type | Description |
|---|---|
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:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The title to apply. |
required |
filename
|
str
|
The filename of the entry. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
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:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The title to apply. |
required |
dest_path
|
str
|
The filename of the entry. |
required |
template
|
Template
|
The template to fill. |
required |
entries
|
List[Entry]
|
A list of entry metadata. |
required |
topic_format
|
str
|
The format of topics to use. |
'[[:topic:]]'
|
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:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
PlainStorage
|
The storage containing all entries. |
required |
feed_type
|
str
|
The type of the feed ('rss' / 'atom'). |
required |
args
|
Namespace
|
Additional arguments required for generating the feed. |
required |
dest_path
|
str
|
The path to write the RSS feed to. |
required |
get_args(arguments=None)
Parse command line arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
List[str]
|
Command line arguments to parse. |
None
|
Returns:
| Type | Description |
|---|---|
Namespace
|
argparse.Namespace: Parsed command line arguments. |
collect_files_sorted(input_arg)
Collects the files defined by the given input definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_arg
|
str
|
The definition of the files to load. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
The list of collected file names. |
main(arguments=None)
The main method using parameters from the command line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
List[str]
|
A list of command line arguments. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The exit code (0 for success). |
script_run()
Execute from command line.