paul@84 | 1 | #!/usr/bin/env python |
paul@84 | 2 | |
paul@174 | 3 | """ |
paul@174 | 4 | Moin wiki format converter. |
paul@174 | 5 | |
paul@174 | 6 | Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@174 | 7 | |
paul@174 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@174 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@174 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@174 | 11 | version. |
paul@174 | 12 | |
paul@174 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@174 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@174 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@174 | 16 | details. |
paul@174 | 17 | |
paul@174 | 18 | You should have received a copy of the GNU General Public License along with |
paul@174 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@174 | 20 | """ |
paul@174 | 21 | |
paul@165 | 22 | from moinformat import make_parser, make_serialiser, Metadata, parse, serialise |
paul@84 | 23 | from os.path import split |
paul@84 | 24 | import sys |
paul@84 | 25 | |
paul@174 | 26 | # Long messages. |
paul@174 | 27 | |
paul@174 | 28 | message_all_with_filenames = """\ |
paul@174 | 29 | Using --all overrides any indicated pagenames. Either --all or the filenames |
paul@174 | 30 | should be omitted.""" |
paul@174 | 31 | |
paul@174 | 32 | message_explicit_pagenames = """\ |
paul@174 | 33 | Explicit pagenames (indicated using --pagename) are only to be specified when |
paul@174 | 34 | providing filenames without an input directory (indicated using --input-dir). |
paul@174 | 35 | |
paul@174 | 36 | To indicate pagenames within an input directory, omit any --pagename flags.""" |
paul@174 | 37 | |
paul@174 | 38 | |
paul@174 | 39 | |
paul@174 | 40 | # Options management. |
paul@174 | 41 | |
paul@133 | 42 | def getmapping(mappings): |
paul@174 | 43 | |
paul@174 | 44 | """ |
paul@174 | 45 | Return the given 'mappings' - a collection of key-then-value items - as a |
paul@174 | 46 | dictionary. |
paul@174 | 47 | """ |
paul@174 | 48 | |
paul@133 | 49 | mapping = {} |
paul@133 | 50 | key = None |
paul@133 | 51 | |
paul@133 | 52 | for arg in mappings: |
paul@133 | 53 | if key is None: |
paul@133 | 54 | key = arg |
paul@133 | 55 | else: |
paul@133 | 56 | mapping[key] = arg |
paul@133 | 57 | key = None |
paul@133 | 58 | |
paul@133 | 59 | return mapping |
paul@133 | 60 | |
paul@165 | 61 | def getvalue(values, default=None): |
paul@174 | 62 | |
paul@174 | 63 | """ |
paul@174 | 64 | Return the first value from 'values' or 'default' if 'values' is empty or |
paul@174 | 65 | the first value tests as false. |
paul@174 | 66 | """ |
paul@174 | 67 | |
paul@165 | 68 | return values and values[0] or default |
paul@133 | 69 | |
paul@174 | 70 | |
paul@174 | 71 | |
paul@174 | 72 | # Main program. |
paul@174 | 73 | |
paul@84 | 74 | def main(): |
paul@174 | 75 | |
paul@174 | 76 | "Interpret program options and perform the conversion." |
paul@174 | 77 | |
paul@84 | 78 | dirname, progname = split(sys.argv[0]) |
paul@84 | 79 | args = sys.argv[1:] |
paul@84 | 80 | |
paul@186 | 81 | if "--help" in args: |
paul@186 | 82 | show_help(progname) |
paul@186 | 83 | sys.exit(0) |
paul@186 | 84 | |
paul@104 | 85 | # Option values. |
paul@104 | 86 | |
paul@84 | 87 | l = filenames = [] |
paul@84 | 88 | formats = [] |
paul@133 | 89 | input_dir_types = [] |
paul@133 | 90 | input_dirs = [] |
paul@104 | 91 | input_encodings = [] |
paul@133 | 92 | input_page_seps = [] |
paul@97 | 93 | mappings = [] |
paul@104 | 94 | output_dirs = [] |
paul@104 | 95 | output_encodings = [] |
paul@161 | 96 | theme_names = [] |
paul@104 | 97 | pagenames = [] |
paul@165 | 98 | root_pagenames = [] |
paul@104 | 99 | |
paul@104 | 100 | # Flags. |
paul@104 | 101 | |
paul@133 | 102 | all = False |
paul@161 | 103 | fragment = False |
paul@104 | 104 | macros = False |
paul@84 | 105 | tree = False |
paul@84 | 106 | |
paul@84 | 107 | for arg in args: |
paul@84 | 108 | |
paul@84 | 109 | # Detect tree output. |
paul@84 | 110 | |
paul@84 | 111 | if arg == "--tree": |
paul@84 | 112 | tree = True |
paul@84 | 113 | |
paul@89 | 114 | # Detect macro evaluation. |
paul@89 | 115 | |
paul@89 | 116 | elif arg == "--macros": |
paul@89 | 117 | macros = True |
paul@89 | 118 | |
paul@133 | 119 | # Detect all documents. |
paul@133 | 120 | |
paul@133 | 121 | elif arg == "--all": |
paul@133 | 122 | all = True |
paul@133 | 123 | |
paul@161 | 124 | # Detect fragment output (if serialising). |
paul@161 | 125 | |
paul@161 | 126 | elif arg == "--fragment": |
paul@161 | 127 | fragment = True |
paul@161 | 128 | |
paul@91 | 129 | # Switch to collecting formats. |
paul@84 | 130 | |
paul@84 | 131 | elif arg == "--format": |
paul@84 | 132 | l = formats |
paul@84 | 133 | continue |
paul@84 | 134 | |
paul@133 | 135 | # Switch to collecting input locations. |
paul@133 | 136 | |
paul@133 | 137 | elif arg == "--input-dir": |
paul@133 | 138 | l = input_dirs |
paul@133 | 139 | continue |
paul@133 | 140 | |
paul@133 | 141 | # Switch to collecting input context types. |
paul@133 | 142 | |
paul@133 | 143 | elif arg == "--input-dir-type": |
paul@133 | 144 | l = input_dir_types |
paul@133 | 145 | continue |
paul@133 | 146 | |
paul@104 | 147 | # Switch to collecting input encodings. |
paul@104 | 148 | |
paul@104 | 149 | elif arg == "--input-encoding": |
paul@104 | 150 | l = input_encodings |
paul@104 | 151 | continue |
paul@104 | 152 | |
paul@133 | 153 | # Switch to collecting input page hierarchy separators. |
paul@133 | 154 | |
paul@133 | 155 | elif arg == "--input-page-sep": |
paul@133 | 156 | l = input_page_seps |
paul@133 | 157 | continue |
paul@133 | 158 | |
paul@97 | 159 | # Switch to collecting mappings. |
paul@97 | 160 | |
paul@97 | 161 | elif arg == "--mapping": |
paul@97 | 162 | l = mappings |
paul@97 | 163 | continue |
paul@97 | 164 | |
paul@100 | 165 | # Switch to collecting output locations. |
paul@100 | 166 | |
paul@104 | 167 | elif arg == "--output-dir": |
paul@104 | 168 | l = output_dirs |
paul@104 | 169 | continue |
paul@104 | 170 | |
paul@104 | 171 | # Switch to collecting output encodings. |
paul@104 | 172 | |
paul@104 | 173 | elif arg == "--output-encoding": |
paul@104 | 174 | l = output_encodings |
paul@100 | 175 | continue |
paul@100 | 176 | |
paul@91 | 177 | # Switch to collecting page names. |
paul@91 | 178 | |
paul@91 | 179 | elif arg == "--pagename": |
paul@91 | 180 | l = pagenames |
paul@91 | 181 | continue |
paul@91 | 182 | |
paul@165 | 183 | # Switch to collecting root page names. |
paul@165 | 184 | |
paul@165 | 185 | elif arg == "--root": |
paul@165 | 186 | l = root_pagenames |
paul@165 | 187 | continue |
paul@165 | 188 | |
paul@161 | 189 | # Switch to collecting theme names. |
paul@161 | 190 | |
paul@161 | 191 | elif arg == "--theme": |
paul@161 | 192 | l = theme_names |
paul@161 | 193 | continue |
paul@161 | 194 | |
paul@84 | 195 | # Collect options and arguments. |
paul@84 | 196 | |
paul@84 | 197 | else: |
paul@84 | 198 | l.append(arg) |
paul@84 | 199 | |
paul@100 | 200 | # Collect multiple mappings. |
paul@100 | 201 | |
paul@97 | 202 | if l is mappings: |
paul@97 | 203 | continue |
paul@97 | 204 | |
paul@84 | 205 | # Collect filenames normally. |
paul@84 | 206 | |
paul@84 | 207 | l = filenames |
paul@84 | 208 | |
paul@91 | 209 | format = formats and formats[0] or "html" |
paul@133 | 210 | input_dir = getvalue(input_dirs) |
paul@133 | 211 | output_dir = getvalue(output_dirs) |
paul@104 | 212 | |
paul@165 | 213 | # Define metadata. |
paul@97 | 214 | |
paul@165 | 215 | metadata = Metadata({ |
paul@165 | 216 | "input_context" : input_dir and \ |
paul@165 | 217 | getvalue(input_dir_types, "directory") or \ |
paul@165 | 218 | "standalone", |
paul@165 | 219 | "input_encoding" : getvalue(input_encodings), |
paul@165 | 220 | "input_filename" : input_dir, |
paul@165 | 221 | "input_separator" : getvalue(input_page_seps), |
paul@165 | 222 | "link_format" : format, |
paul@165 | 223 | "mapping" : getmapping(mappings), |
paul@165 | 224 | "output_context" : output_dir and "directory" or "standalone", |
paul@165 | 225 | "output_encoding" : getvalue(output_encodings), |
paul@165 | 226 | "output_format" : format, |
paul@165 | 227 | "output_filename" : output_dir, |
paul@165 | 228 | "root_pagename" : getvalue(root_pagenames, "FrontPage"), |
paul@165 | 229 | "theme_name" : not fragment and \ |
paul@165 | 230 | "%s.%s" % (getvalue(theme_names, "default"), format) or None, |
paul@165 | 231 | }) |
paul@133 | 232 | |
paul@165 | 233 | # Define the input context and theme. |
paul@161 | 234 | |
paul@165 | 235 | input = metadata.get_input() |
paul@165 | 236 | theme = metadata.get_theme() |
paul@161 | 237 | |
paul@133 | 238 | # Treat filenames as pagenames if an input directory is indicated and if no |
paul@133 | 239 | # pagenames are explicitly specified. |
paul@104 | 240 | |
paul@133 | 241 | if input_dir: |
paul@133 | 242 | if pagenames: |
paul@174 | 243 | print >>sys.stderr, message_explicit_pagenames |
paul@133 | 244 | sys.exit(1) |
paul@104 | 245 | |
paul@133 | 246 | if all: |
paul@133 | 247 | if filenames: |
paul@174 | 248 | print >>sys.stderr, message_all_with_filenames |
paul@133 | 249 | sys.exit(1) |
paul@133 | 250 | else: |
paul@133 | 251 | filenames = input.all() |
paul@104 | 252 | |
paul@133 | 253 | pagenames = filenames |
paul@133 | 254 | filenames = [] |
paul@133 | 255 | |
paul@133 | 256 | # Open each file or page, parse the content, serialise the document. |
paul@104 | 257 | |
paul@133 | 258 | for pagename, filename in map(None, pagenames, filenames): |
paul@133 | 259 | |
paul@133 | 260 | # Define a pagename if missing. |
paul@133 | 261 | |
paul@133 | 262 | pagename = pagename or split(filename)[-1] |
paul@165 | 263 | metadata.set("pagename", pagename) |
paul@89 | 264 | |
paul@133 | 265 | # Read either from a filename or using a pagename. |
paul@133 | 266 | |
paul@133 | 267 | if filename: |
paul@133 | 268 | pagetext = input.readfile(filename) |
paul@133 | 269 | else: |
paul@133 | 270 | pagetext = input.readpage(pagename) |
paul@133 | 271 | |
paul@133 | 272 | # Parse the page content. |
paul@104 | 273 | |
paul@165 | 274 | p = make_parser(metadata) |
paul@133 | 275 | d = parse(pagetext, p) |
paul@133 | 276 | |
paul@133 | 277 | if macros: |
paul@133 | 278 | p.evaluate_macros() |
paul@104 | 279 | |
paul@133 | 280 | # Show a document tree for debugging purposes, if requested. |
paul@133 | 281 | |
paul@133 | 282 | if tree: |
paul@133 | 283 | print d.prettyprint() |
paul@133 | 284 | continue |
paul@133 | 285 | |
paul@133 | 286 | # Otherwise, serialise the document. |
paul@104 | 287 | |
paul@104 | 288 | # Obtain a serialiser using the configuration. |
paul@104 | 289 | |
paul@165 | 290 | serialiser = make_serialiser(metadata) |
paul@133 | 291 | outtext = serialise(d, serialiser) |
paul@133 | 292 | |
paul@161 | 293 | # With a theme, apply it to the text. |
paul@161 | 294 | |
paul@161 | 295 | if theme: |
paul@161 | 296 | outtext = theme.apply(outtext) |
paul@161 | 297 | |
paul@133 | 298 | # If reading from a file, show the result. Otherwise, write to the |
paul@133 | 299 | # output context. |
paul@133 | 300 | |
paul@165 | 301 | output = metadata.get_output() |
paul@165 | 302 | |
paul@133 | 303 | if not output.can_write(): |
paul@133 | 304 | print outtext |
paul@133 | 305 | else: |
paul@133 | 306 | output.writepage(outtext, pagename) |
paul@133 | 307 | print >>sys.stderr, pagename |
paul@84 | 308 | |
paul@161 | 309 | # Install any theme resources. |
paul@161 | 310 | |
paul@161 | 311 | if theme: |
paul@161 | 312 | theme.install_resources() |
paul@161 | 313 | |
paul@186 | 314 | def show_help(progname): |
paul@186 | 315 | |
paul@186 | 316 | "Show the help text." |
paul@186 | 317 | |
paul@186 | 318 | print >>sys.stderr, help_text % progname |
paul@186 | 319 | |
paul@186 | 320 | help_text = """\ |
paul@186 | 321 | Usage: %s [ <options> ] ( --all | <filename>... ) |
paul@186 | 322 | |
paul@186 | 323 | Input options: |
paul@186 | 324 | |
paul@186 | 325 | --all Detect all document files in the specified input directory |
paul@186 | 326 | --input-dir Indicate an input directory containing document files |
paul@186 | 327 | --input-dir-type Indicate the type of input directory involved |
paul@186 | 328 | (default: directory) |
paul@186 | 329 | --input-encoding Indicate the character encoding used in document files |
paul@186 | 330 | --input-page-sep Indicate the separator used in filenames to encode |
paul@186 | 331 | hierarchical relationships (subpages and descendant pages) |
paul@186 | 332 | --pagename Indicate the page name corresponding to an indicated |
paul@186 | 333 | filename, with each successive instance of this option |
paul@186 | 334 | corresponding to each successive filename instance |
paul@186 | 335 | |
paul@186 | 336 | Output options: |
paul@186 | 337 | |
paul@186 | 338 | --format Indicate the format to be used for serialised documents |
paul@186 | 339 | (default: html) |
paul@186 | 340 | --fragment Indicates that an output fragment, not an entire document, |
paul@186 | 341 | is to be generated, skipping any theming activities |
paul@186 | 342 | --output-dir Indicate an output directory to contain serialised document |
paul@186 | 343 | files |
paul@186 | 344 | --output-encoding Indicate the character encoding used in serialised document |
paul@186 | 345 | files |
paul@186 | 346 | --theme Indicate a theme for serialised documents, typically |
paul@186 | 347 | requiring an output directory to be useful |
paul@186 | 348 | --tree Produce a document tree representation on standard output |
paul@186 | 349 | instead of generating output files |
paul@186 | 350 | |
paul@186 | 351 | Configuration options: |
paul@186 | 352 | |
paul@186 | 353 | --macros Perform macro evaluation/expansion before serialising |
paul@186 | 354 | documents |
paul@186 | 355 | --mapping Indicate a name and corresponding URL to be used to |
paul@186 | 356 | translate interwiki links |
paul@186 | 357 | --root Indicate the root page name to be used |
paul@186 | 358 | (default: FrontPage) |
paul@186 | 359 | """ |
paul@186 | 360 | |
paul@84 | 361 | if __name__ == "__main__": |
paul@84 | 362 | main() |
paul@84 | 363 | |
paul@84 | 364 | # vim: tabstop=4 expandtab shiftwidth=4 |