paul@347 | 1 | #!/usr/bin/env python |
paul@347 | 2 | |
paul@347 | 3 | """ |
paul@347 | 4 | Document format translators. |
paul@347 | 5 | |
paul@347 | 6 | Copyright (C) 2017, 2018, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@347 | 7 | |
paul@347 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@347 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@347 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@347 | 11 | version. |
paul@347 | 12 | |
paul@347 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@347 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@347 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@347 | 16 | details. |
paul@347 | 17 | |
paul@347 | 18 | You should have received a copy of the GNU General Public License along with |
paul@347 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@347 | 20 | """ |
paul@347 | 21 | |
paul@347 | 22 | from moinformat.translators.manifest import translators |
paul@347 | 23 | |
paul@347 | 24 | # Top-level functions. |
paul@347 | 25 | |
paul@347 | 26 | def get_translator(name, doctype=None): |
paul@347 | 27 | |
paul@347 | 28 | """ |
paul@347 | 29 | Return a translator class producing nodes whose document type has the given |
paul@347 | 30 | 'name'. If 'doctype' is indicated, obtain a translator class specific to |
paul@347 | 31 | that document type. Otherwise, a general translator for Moin content is |
paul@347 | 32 | obtained. |
paul@347 | 33 | """ |
paul@347 | 34 | |
paul@347 | 35 | return translators["%s.%s" % (name, doctype or "moin")] |
paul@347 | 36 | |
paul@347 | 37 | def make_translator(metadata, doctype=None): |
paul@347 | 38 | |
paul@347 | 39 | """ |
paul@347 | 40 | Return a translator instance using the given 'metadata' and optional output |
paul@347 | 41 | 'doctype'. |
paul@347 | 42 | """ |
paul@347 | 43 | |
paul@347 | 44 | return metadata.get_translator(doctype) |
paul@347 | 45 | |
paul@347 | 46 | def translate(doc, translator): |
paul@347 | 47 | |
paul@347 | 48 | "Translate 'doc' using the given 'translator' instance." |
paul@347 | 49 | |
paul@347 | 50 | return doc.visit(translator) |
paul@347 | 51 | |
paul@347 | 52 | # vim: tabstop=4 expandtab shiftwidth=4 |