MoinLight

Annotated moinformat/serialisers/__init__.py

104:8d437cdf2381
2018-07-30 Paul Boddie Introduced Unicode strings as the typical form of document data for processing, employing output encodings when serialising output documents. Introduced input contexts which provide details of input document encodings and document source locations. Introduced the familiar manifest and lookup mechanisms for input and output contexts. Provided support for standalone contexts, with the output context suppressing the generation of additional files. Moved common directory functionality to a separate utility module. Updated the conversion script and test framework, adding encoding-related tests.
paul@38 1
#!/usr/bin/env python
paul@38 2
paul@38 3
"""
paul@38 4
Moin wiki serialisers.
paul@38 5
paul@46 6
Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
paul@38 7
paul@38 8
This program is free software; you can redistribute it and/or modify it under
paul@38 9
the terms of the GNU General Public License as published by the Free Software
paul@38 10
Foundation; either version 3 of the License, or (at your option) any later
paul@38 11
version.
paul@38 12
paul@38 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@38 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@38 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@38 16
details.
paul@38 17
paul@38 18
You should have received a copy of the GNU General Public License along with
paul@38 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@38 20
"""
paul@38 21
paul@96 22
from moinformat.links import make_linker
paul@104 23
from moinformat.output import make_output
paul@40 24
from moinformat.serialisers.manifest import serialisers
paul@85 25
from moinformat.serialisers.moin.moin import MoinSerialiser
paul@100 26
from os.path import curdir
paul@38 27
paul@38 28
# Top-level functions.
paul@38 29
paul@96 30
def get_serialiser(name):
paul@96 31
paul@96 32
    "Return the main serialiser class for the format having the given 'name'."
paul@96 33
paul@96 34
    return serialisers["%s.moin" % name]
paul@96 35
paul@100 36
def make_serialiser(name, output=None, linker=None):
paul@96 37
paul@96 38
    """
paul@96 39
    Return a serialiser instance for the format having the given 'name'.
paul@96 40
paul@100 41
    The optional 'output' context is used to control where separate resources
paul@104 42
    are stored, with the default being no storage of such resources.
paul@100 43
paul@96 44
    The optional 'linker' is used to control which linking scheme is used with
paul@96 45
    the serialiser, with the default having the same name as the serialiser.
paul@96 46
    """
paul@96 47
paul@104 48
    output = output or make_output("standalone")
paul@96 49
    linker = linker or make_linker(name, "")
paul@100 50
    return get_serialiser(name)(output, serialisers, linker)
paul@96 51
paul@94 52
def serialise(doc, serialiser=None):
paul@46 53
paul@94 54
    """
paul@94 55
    Serialise 'doc' using the given 'serialiser' instance or the Moin serialiser
paul@94 56
    if omitted.
paul@94 57
    """
paul@46 58
paul@100 59
    if not serialiser:
paul@104 60
        output = make_output("standalone")
paul@100 61
        serialiser = MoinSerialiser(output, serialisers)
paul@100 62
paul@94 63
    doc.to_string(serialiser)
paul@100 64
    return serialiser.get_output()
paul@38 65
paul@38 66
# vim: tabstop=4 expandtab shiftwidth=4