Developer’s Reference¶
The power of Foliant is in its extensions. Foliant's ecosystem consists of many beautiful tools for technical writers, but there is still a lot to be done. You are welcome to contribute to Foliant and its extensions.
This article contains the reference of the main classes and functions available in Foliant Core. As an extension developer, you will be using them to write your own preprocessors, backends, CLI- and config-extensions.
If you are new to extending Foliant, we suggest you to take a look at the Creating a Preprocessor tutorial first.
Official Foliant extensions live in Git repositories inside the foliant-docs GitHub group. Check out their source code to find out different approaches to solving techwriters' problems.
The repo of Foliant Core is called foliant. The names of Foliant extensions' repositories start with the foliantcontrib.
prefix. The repo of this documentation project is called docs.
Core Modules¶
Core modules live in the foliant GitHub repository. Foliant Core itself does not build documentation projects, this job is delegated to extensions. But it defines the base classes for all types of extensions. Each base class offers useful attributes and methods which are described later in this article. For more info on how Foliant works check the Architecture And Basic Design Concepts article.
This section lists all modules in the Foliant Core package.
foliant
:-
backends
: -
preprocessors
:base
— defines the base class for all preprocessors;_unescape
— simple preprocessor that escapes pseudo-XML tags (which are normally recognized by other preprocessors as control sequences) in code examples. If you want an opening tag to be ignored by any preprocessor, precede this tag with the<
character. The_unescape
preprocessor removes these characters before build. Instead of the_unescape
preprocessor, you may use more flexible EscapeCode and UnescapeCode preprocessors;
-
cli
— defines the Foliant’s root classFoliant()
and theentry_point()
method that is used as a starting point for calling Foliant. Nested modules: -
config
:base
— defines the base class for all config extensions;include
— resolves the!include
YAML tag that allows to include the content of additional YAML-files in Foliant config. More info in the Project Configuration article;path
— resolves the!path
,!project_path
and!rep_path
YAML tags. These tags are useful for specifying file paths in Foliant config or tag attributes. More info in the Project Configuration article;
-
utils
— defines basic methods that may be used in different types of extensions.
The make()
Method Arguments¶
The make()
method is defined in the foliant.cli.make
module. This method is called when the user runs foliant make ...
command. For more info on how make
command works check the Project Build Process article.
The make()
method accepts a number of arguments; some of them are then passed to the backends and preprocessors in the build context:
target
(string) — required resulting target of the current build;backend
(string, defaults to an empty string) — the name of the backend that is used for the current build;project_path
(path, defaults to the current directory path) — the path of top-level, “root” directory of the current Foliant project;config_file_name
(string, defaults tofoliant.yml
) — the file name of the Foliant project’s config;quiet
(boolean, default toFalse
) — a flag that prohibits writing toSTDOUT
;keep_tmp
(boolean, defaults toFalse
) — a flag that tells Foliant and its extensions to preserve the temporary working directory, which is used during the build;debug
(boolean, defaults toFalse
) — a flag that tells Foliant and its extensions to log events ofinfo
anddebug
levels in addition to messages ofwarning
,error
, andcritical
levels.
Base Classes¶
Foliant Core provides 4 base classes—one per each type of extension.
BaseBackend()
is defined in thefoliant.backends.base
module. It is the base class for all backends. Each newly developed backend should:- be a module or a package
foliant.backends.<your_backend_name>
; - import the class
BaseBackend()
from thefoliant.backends.base
module; - define its own class called
Backend()
that is inherited fromBaseBackend()
; -
define the method called
make()
within theBackend
class. -
BasePreprocessor()
is defined in thefoliant.preprocessors.base
module. It is the base class for all preprocessors. Each newly developed preprocessor should: - be a module or a package
foliant.preprocessors.<your_preprocessor_name>
; - import the class
BasePreprocessor()
from thefoliant.preprocessors.base
module; - define its own class called
Preprocessor()
that is inherited fromBasePreprocessor()
; -
define the method called
apply()
within the classPreprocessor()
. -
BaseCli()
is defined in thefoliant.cli.base
module. It is the base class for all CLI extensions. Each newly developed CLI extension should: - be a module or a package
foliant.cli.<your_cli_extension_name>
; - import the class
BaseCli()
from thefoliant.cli.base
module; -
define its own class called
Cli()
that is inherited fromBaseCli()
. -
BaseParser()
is defined in thefoliant.config.base
module. It is the base class for all config extensions. Each newly developed config extension should: - be a module or a package
foliant.config.<your_config_extension_name>
; - import the class
BaseParser()
from thefoliant.config.base
module; - define its own class called
Parser()
that is inherited fromBaseParser()
.
The BaseBackend()
Attributes¶
- Class attributes:
targets
(tuple of strings) — names of the targets that the backend can build;required_preprocessors_before
(tuple of strings) — names of the preprocessors that should be applied before all other preprocessors when this backend is used;-
required_preprocessors_after
(tuple of strings) — names of preprocessors that should be applied after all other preprocessors when this backend is used; -
instance variables:
-
context
— a dictionary that contains the build context:project_path
(path) — path to the currently built Foliant project;config
(dictionary) — full config of the currently built Foliant project;target
(string) — the name of the resulting target;backend
(string) — the name of the backend that is used in the current build;
-
config
— full config of the currently built Foliant project. The same ascontext['config']
; project_path
— path to the currently built Foliant project. The same ascontext['project_path']
;working_dir
(path) — the path to the temporary working directory that is used during the build. It is defined asself.project_path / self.config['tmp_dir']
;logger
— the Logger instance of the current build;quiet
(boolean) — ifTrue
, the backend should not write anything to stdout;debug
(boolean) — ifTrue
, the backend should log the messages ofinfo
anddebug
levels.
The BasePreprocessor()
Attributes¶
- Class attributes:
defaults
(dictionary) — default values of options that may be overridden in config;-
tags
(tuple of strings) — names of pseudo-XML tags that are recognized by the preprocessor, without<
and>
characters; -
instance variables:
-
context
— a dictionary that contains the build context:project_path
(path) — path to the currently built Foliant project;config
(dictionary) — full config of the currently built Foliant project;target
(string) — the name of the resulting target;backend
(string) — the name of the backend that is used in the current build;
-
config
— full config of the currently built Foliant project. The same asself.context['config']
; project_path
— path to the currently built Foliant project. The same asself.context['project_path']
;working_dir
(path) — the path to the temporary working directory that is used during the build. It is defined asself.project_path / self.config['tmp_dir']
;logger
— the Logger instance of the current build;quiet
(boolean) — ifTrue
, the backend should not write anything to stdout;debug
(boolean) — ifTrue
, the backend should log the messages ofinfo
anddebug
levels.options
(dictionary) — the preprocessor’s options. Is defined as{**self.defaults, **options}
, whereoptions
is the data that is read from the preprocessor's config in foliant.yml;pattern
— the regular expression pattern that is used to get components of a pseudo-XML tag in an easy way. Defined ifself.tags
is not empty. Provides the RegEx groups with the following names:tag
— captured tag name;options
— captured tag attributes (options) as a string; this string may be converted into a dictionary by using theself.get_options()
method, which is provided by the base class;body
— captured tag body, i.e. the content between the opening and closing tags.
BaseCli()
Attributes¶
- Instance attributes:
logger
— the Logger instance of the current build.
BaseConfig()
Attributes¶
- Instance attributes:
project_path
(path) — the path to the currently built Foliant project;config_path
(path) — the path to the config file of the currently built Foliant project;logger
— the Logger instance of the current build;quiet
(boolean) — ifTrue
, the config extension should not write anything to stdout.