paul@84 | 1 | #!/usr/bin/env python |
paul@84 | 2 | |
paul@174 | 3 | """ |
paul@174 | 4 | Moin wiki format converter. |
paul@174 | 5 | |
paul@312 | 6 | Copyright (C) 2018, 2019, 2021 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@223 | 22 | from moinformat import copy_attachments, errors, make_parser, make_serialiser, \ |
paul@223 | 23 | Metadata, parse, serialise |
paul@84 | 24 | from os.path import split |
paul@84 | 25 | import sys |
paul@84 | 26 | |
paul@174 | 27 | # Long messages. |
paul@174 | 28 | |
paul@174 | 29 | message_all_with_filenames = """\ |
paul@174 | 30 | Using --all overrides any indicated pagenames. Either --all or the filenames |
paul@174 | 31 | should be omitted.""" |
paul@174 | 32 | |
paul@174 | 33 | message_explicit_pagenames = """\ |
paul@174 | 34 | Explicit pagenames (indicated using --pagename) are only to be specified when |
paul@174 | 35 | providing filenames without an input directory (indicated using --input-dir). |
paul@174 | 36 | |
paul@174 | 37 | To indicate pagenames within an input directory, omit any --pagename flags.""" |
paul@174 | 38 | |
paul@174 | 39 | |
paul@174 | 40 | |
paul@174 | 41 | # Options management. |
paul@174 | 42 | |
paul@133 | 43 | def getmapping(mappings): |
paul@174 | 44 | |
paul@174 | 45 | """ |
paul@174 | 46 | Return the given 'mappings' - a collection of key-then-value items - as a |
paul@174 | 47 | dictionary. |
paul@174 | 48 | """ |
paul@174 | 49 | |
paul@133 | 50 | mapping = {} |
paul@133 | 51 | key = None |
paul@133 | 52 | |
paul@133 | 53 | for arg in mappings: |
paul@133 | 54 | if key is None: |
paul@133 | 55 | key = arg |
paul@133 | 56 | else: |
paul@133 | 57 | mapping[key] = arg |
paul@133 | 58 | key = None |
paul@133 | 59 | |
paul@133 | 60 | return mapping |
paul@133 | 61 | |
paul@165 | 62 | def getvalue(values, default=None): |
paul@174 | 63 | |
paul@174 | 64 | """ |
paul@174 | 65 | Return the first value from 'values' or 'default' if 'values' is empty or |
paul@174 | 66 | the first value tests as false. |
paul@174 | 67 | """ |
paul@174 | 68 | |
paul@165 | 69 | return values and values[0] or default |
paul@133 | 70 | |
paul@174 | 71 | |
paul@174 | 72 | |
paul@174 | 73 | # Main program. |
paul@174 | 74 | |
paul@84 | 75 | def main(): |
paul@174 | 76 | |
paul@174 | 77 | "Interpret program options and perform the conversion." |
paul@174 | 78 | |
paul@84 | 79 | dirname, progname = split(sys.argv[0]) |
paul@84 | 80 | args = sys.argv[1:] |
paul@84 | 81 | |
paul@186 | 82 | if "--help" in args: |
paul@186 | 83 | show_help(progname) |
paul@186 | 84 | sys.exit(0) |
paul@186 | 85 | |
paul@104 | 86 | # Option values. |
paul@104 | 87 | |
paul@215 | 88 | attachments_dir = [] |
paul@196 | 89 | document_indexes = [] |
paul@196 | 90 | filenames = [] |
paul@84 | 91 | formats = [] |
paul@133 | 92 | input_dir_types = [] |
paul@133 | 93 | input_dirs = [] |
paul@104 | 94 | input_encodings = [] |
paul@133 | 95 | input_page_seps = [] |
paul@97 | 96 | mappings = [] |
paul@104 | 97 | output_dirs = [] |
paul@104 | 98 | output_encodings = [] |
paul@312 | 99 | output_page_seps = [] |
paul@104 | 100 | pagenames = [] |
paul@165 | 101 | root_pagenames = [] |
paul@196 | 102 | theme_names = [] |
paul@196 | 103 | |
paul@196 | 104 | # Obtain filenames by default. |
paul@196 | 105 | |
paul@196 | 106 | l = filenames |
paul@104 | 107 | |
paul@104 | 108 | # Flags. |
paul@104 | 109 | |
paul@133 | 110 | all = False |
paul@278 | 111 | bundle = False |
paul@282 | 112 | common = False |
paul@161 | 113 | fragment = False |
paul@104 | 114 | macros = False |
paul@282 | 115 | no_inline = False |
paul@84 | 116 | tree = False |
paul@84 | 117 | |
paul@84 | 118 | for arg in args: |
paul@84 | 119 | |
paul@215 | 120 | # Flags with no following arguments. |
paul@215 | 121 | |
paul@215 | 122 | # Detect all documents. |
paul@84 | 123 | |
paul@215 | 124 | if arg == "--all": |
paul@215 | 125 | all = True |
paul@215 | 126 | |
paul@278 | 127 | # Detect resource bundling. |
paul@278 | 128 | |
paul@278 | 129 | elif arg == "--bundle": |
paul@278 | 130 | bundle = True |
paul@278 | 131 | |
paul@282 | 132 | # Detect use of a common attachments directory. |
paul@282 | 133 | |
paul@282 | 134 | elif arg == "--common": |
paul@282 | 135 | common = True |
paul@282 | 136 | |
paul@215 | 137 | # Detect fragment output (if serialising). |
paul@215 | 138 | |
paul@215 | 139 | elif arg == "--fragment": |
paul@215 | 140 | fragment = True |
paul@84 | 141 | |
paul@89 | 142 | # Detect macro evaluation. |
paul@89 | 143 | |
paul@89 | 144 | elif arg == "--macros": |
paul@89 | 145 | macros = True |
paul@89 | 146 | |
paul@282 | 147 | # Detect suppression of inline objects. |
paul@282 | 148 | |
paul@282 | 149 | elif arg == "--no-inline": |
paul@282 | 150 | no_inline = True |
paul@282 | 151 | |
paul@215 | 152 | # Detect tree output. |
paul@215 | 153 | |
paul@215 | 154 | elif arg == "--tree": |
paul@215 | 155 | tree = True |
paul@133 | 156 | |
paul@215 | 157 | # Options with following arguments. |
paul@215 | 158 | |
paul@215 | 159 | # Switch to document index. |
paul@215 | 160 | |
paul@215 | 161 | elif arg == "--attachments-dir": |
paul@215 | 162 | l = attachments_dir |
paul@215 | 163 | continue |
paul@133 | 164 | |
paul@196 | 165 | # Switch to document index. |
paul@196 | 166 | |
paul@196 | 167 | elif arg == "--document-index": |
paul@196 | 168 | l = document_indexes |
paul@196 | 169 | continue |
paul@196 | 170 | |
paul@91 | 171 | # Switch to collecting formats. |
paul@84 | 172 | |
paul@84 | 173 | elif arg == "--format": |
paul@84 | 174 | l = formats |
paul@84 | 175 | continue |
paul@84 | 176 | |
paul@133 | 177 | # Switch to collecting input locations. |
paul@133 | 178 | |
paul@133 | 179 | elif arg == "--input-dir": |
paul@133 | 180 | l = input_dirs |
paul@133 | 181 | continue |
paul@133 | 182 | |
paul@133 | 183 | # Switch to collecting input context types. |
paul@133 | 184 | |
paul@133 | 185 | elif arg == "--input-dir-type": |
paul@133 | 186 | l = input_dir_types |
paul@133 | 187 | continue |
paul@133 | 188 | |
paul@104 | 189 | # Switch to collecting input encodings. |
paul@104 | 190 | |
paul@104 | 191 | elif arg == "--input-encoding": |
paul@104 | 192 | l = input_encodings |
paul@104 | 193 | continue |
paul@104 | 194 | |
paul@133 | 195 | # Switch to collecting input page hierarchy separators. |
paul@133 | 196 | |
paul@133 | 197 | elif arg == "--input-page-sep": |
paul@133 | 198 | l = input_page_seps |
paul@133 | 199 | continue |
paul@133 | 200 | |
paul@97 | 201 | # Switch to collecting mappings. |
paul@97 | 202 | |
paul@97 | 203 | elif arg == "--mapping": |
paul@97 | 204 | l = mappings |
paul@97 | 205 | continue |
paul@97 | 206 | |
paul@100 | 207 | # Switch to collecting output locations. |
paul@100 | 208 | |
paul@104 | 209 | elif arg == "--output-dir": |
paul@104 | 210 | l = output_dirs |
paul@104 | 211 | continue |
paul@104 | 212 | |
paul@104 | 213 | # Switch to collecting output encodings. |
paul@104 | 214 | |
paul@104 | 215 | elif arg == "--output-encoding": |
paul@104 | 216 | l = output_encodings |
paul@100 | 217 | continue |
paul@100 | 218 | |
paul@312 | 219 | # Switch to collecting output page hierarchy separators. |
paul@312 | 220 | |
paul@312 | 221 | elif arg == "--output-page-sep": |
paul@312 | 222 | l = output_page_seps |
paul@312 | 223 | continue |
paul@312 | 224 | |
paul@91 | 225 | # Switch to collecting page names. |
paul@91 | 226 | |
paul@91 | 227 | elif arg == "--pagename": |
paul@91 | 228 | l = pagenames |
paul@91 | 229 | continue |
paul@91 | 230 | |
paul@165 | 231 | # Switch to collecting root page names. |
paul@165 | 232 | |
paul@165 | 233 | elif arg == "--root": |
paul@165 | 234 | l = root_pagenames |
paul@165 | 235 | continue |
paul@165 | 236 | |
paul@161 | 237 | # Switch to collecting theme names. |
paul@161 | 238 | |
paul@161 | 239 | elif arg == "--theme": |
paul@161 | 240 | l = theme_names |
paul@161 | 241 | continue |
paul@161 | 242 | |
paul@84 | 243 | # Collect options and arguments. |
paul@84 | 244 | |
paul@84 | 245 | else: |
paul@84 | 246 | l.append(arg) |
paul@84 | 247 | |
paul@100 | 248 | # Collect multiple mappings. |
paul@100 | 249 | |
paul@97 | 250 | if l is mappings: |
paul@97 | 251 | continue |
paul@97 | 252 | |
paul@84 | 253 | # Collect filenames normally. |
paul@84 | 254 | |
paul@84 | 255 | l = filenames |
paul@84 | 256 | |
paul@91 | 257 | format = formats and formats[0] or "html" |
paul@133 | 258 | input_dir = getvalue(input_dirs) |
paul@133 | 259 | output_dir = getvalue(output_dirs) |
paul@104 | 260 | |
paul@165 | 261 | # Define metadata. |
paul@97 | 262 | |
paul@165 | 263 | metadata = Metadata({ |
paul@215 | 264 | "attachments" : getvalue(attachments_dir, "attachments"), |
paul@278 | 265 | "bundle" : bundle, |
paul@282 | 266 | "common_attachments": common, |
paul@196 | 267 | "document_index" : getvalue(document_indexes), |
paul@165 | 268 | "input_context" : input_dir and \ |
paul@165 | 269 | getvalue(input_dir_types, "directory") or \ |
paul@165 | 270 | "standalone", |
paul@165 | 271 | "input_encoding" : getvalue(input_encodings), |
paul@165 | 272 | "input_filename" : input_dir, |
paul@165 | 273 | "input_separator" : getvalue(input_page_seps), |
paul@165 | 274 | "link_format" : format, |
paul@165 | 275 | "mapping" : getmapping(mappings), |
paul@282 | 276 | "no_inline" : no_inline, |
paul@165 | 277 | "output_context" : output_dir and "directory" or "standalone", |
paul@165 | 278 | "output_encoding" : getvalue(output_encodings), |
paul@165 | 279 | "output_format" : format, |
paul@165 | 280 | "output_filename" : output_dir, |
paul@312 | 281 | "output_separator" : getvalue(output_page_seps), |
paul@165 | 282 | "root_pagename" : getvalue(root_pagenames, "FrontPage"), |
paul@165 | 283 | "theme_name" : not fragment and \ |
paul@165 | 284 | "%s.%s" % (getvalue(theme_names, "default"), format) or None, |
paul@165 | 285 | }) |
paul@133 | 286 | |
paul@165 | 287 | # Define the input context and theme. |
paul@161 | 288 | |
paul@165 | 289 | input = metadata.get_input() |
paul@165 | 290 | theme = metadata.get_theme() |
paul@161 | 291 | |
paul@133 | 292 | # Treat filenames as pagenames if an input directory is indicated and if no |
paul@133 | 293 | # pagenames are explicitly specified. |
paul@104 | 294 | |
paul@133 | 295 | if input_dir: |
paul@133 | 296 | if pagenames: |
paul@174 | 297 | print >>sys.stderr, message_explicit_pagenames |
paul@133 | 298 | sys.exit(1) |
paul@104 | 299 | |
paul@133 | 300 | if all: |
paul@133 | 301 | if filenames: |
paul@174 | 302 | print >>sys.stderr, message_all_with_filenames |
paul@133 | 303 | sys.exit(1) |
paul@133 | 304 | else: |
paul@133 | 305 | filenames = input.all() |
paul@104 | 306 | |
paul@133 | 307 | pagenames = filenames |
paul@133 | 308 | filenames = [] |
paul@133 | 309 | |
paul@133 | 310 | # Open each file or page, parse the content, serialise the document. |
paul@104 | 311 | |
paul@133 | 312 | for pagename, filename in map(None, pagenames, filenames): |
paul@133 | 313 | |
paul@133 | 314 | # Define a pagename if missing. |
paul@133 | 315 | |
paul@133 | 316 | pagename = pagename or split(filename)[-1] |
paul@165 | 317 | metadata.set("pagename", pagename) |
paul@89 | 318 | |
paul@133 | 319 | # Read either from a filename or using a pagename. |
paul@133 | 320 | |
paul@133 | 321 | if filename: |
paul@133 | 322 | pagetext = input.readfile(filename) |
paul@133 | 323 | else: |
paul@133 | 324 | pagetext = input.readpage(pagename) |
paul@133 | 325 | |
paul@133 | 326 | # Parse the page content. |
paul@104 | 327 | |
paul@165 | 328 | p = make_parser(metadata) |
paul@133 | 329 | d = parse(pagetext, p) |
paul@133 | 330 | |
paul@133 | 331 | if macros: |
paul@133 | 332 | p.evaluate_macros() |
paul@104 | 333 | |
paul@199 | 334 | p.update_metadata(metadata) |
paul@199 | 335 | |
paul@133 | 336 | # Show a document tree for debugging purposes, if requested. |
paul@133 | 337 | |
paul@133 | 338 | if tree: |
paul@133 | 339 | print d.prettyprint() |
paul@133 | 340 | continue |
paul@133 | 341 | |
paul@133 | 342 | # Otherwise, serialise the document. |
paul@104 | 343 | |
paul@104 | 344 | # Obtain a serialiser using the configuration. |
paul@104 | 345 | |
paul@165 | 346 | serialiser = make_serialiser(metadata) |
paul@133 | 347 | outtext = serialise(d, serialiser) |
paul@133 | 348 | |
paul@161 | 349 | # With a theme, apply it to the text. |
paul@161 | 350 | |
paul@161 | 351 | if theme: |
paul@161 | 352 | outtext = theme.apply(outtext) |
paul@161 | 353 | |
paul@133 | 354 | # If reading from a file, show the result. Otherwise, write to the |
paul@133 | 355 | # output context. |
paul@133 | 356 | |
paul@165 | 357 | output = metadata.get_output() |
paul@165 | 358 | |
paul@133 | 359 | if not output.can_write(): |
paul@133 | 360 | print outtext |
paul@133 | 361 | else: |
paul@133 | 362 | output.writepage(outtext, pagename) |
paul@133 | 363 | print >>sys.stderr, pagename |
paul@84 | 364 | |
paul@232 | 365 | copy_attachments(p, input, output, all=True) |
paul@223 | 366 | |
paul@161 | 367 | # Install any theme resources. |
paul@161 | 368 | |
paul@161 | 369 | if theme: |
paul@161 | 370 | theme.install_resources() |
paul@161 | 371 | |
paul@186 | 372 | def show_help(progname): |
paul@186 | 373 | |
paul@186 | 374 | "Show the help text." |
paul@186 | 375 | |
paul@186 | 376 | print >>sys.stderr, help_text % progname |
paul@186 | 377 | |
paul@186 | 378 | help_text = """\ |
paul@186 | 379 | Usage: %s [ <options> ] ( --all | <filename>... ) |
paul@186 | 380 | |
paul@198 | 381 | Input file options: |
paul@198 | 382 | |
paul@198 | 383 | --all Detect all document files in the specified input directory |
paul@198 | 384 | |
paul@186 | 385 | Input options: |
paul@186 | 386 | |
paul@282 | 387 | --common Obtain attachments from a common directory for all pages, |
paul@282 | 388 | rather than each page having its own subdirectory of a |
paul@282 | 389 | top-level attachments directory. |
paul@186 | 390 | --input-dir Indicate an input directory containing document files |
paul@186 | 391 | --input-dir-type Indicate the type of input directory involved |
paul@186 | 392 | (default: directory) |
paul@186 | 393 | --input-encoding Indicate the character encoding used in document files |
paul@186 | 394 | --input-page-sep Indicate the separator used in filenames to encode |
paul@186 | 395 | hierarchical relationships (subpages and descendant pages) |
paul@186 | 396 | --pagename Indicate the page name corresponding to an indicated |
paul@186 | 397 | filename, with each successive instance of this option |
paul@186 | 398 | corresponding to each successive filename instance |
paul@186 | 399 | |
paul@186 | 400 | Output options: |
paul@186 | 401 | |
paul@278 | 402 | --bundle Bundle resources such as stylesheets within every document, |
paul@278 | 403 | useful for publishing documents that need to be copied or |
paul@278 | 404 | distributed individually. |
paul@196 | 405 | --document-index Provide a "DocumentIndex" filename to be used in links in |
paul@196 | 406 | HTML format output, useful for local file browsing instead |
paul@196 | 407 | of Web-published content |
paul@186 | 408 | --format Indicate the format to be used for serialised documents |
paul@186 | 409 | (default: html) |
paul@186 | 410 | --fragment Indicates that an output fragment, not an entire document, |
paul@186 | 411 | is to be generated, skipping any theming activities |
paul@282 | 412 | --no-inline Suppress inline objects in serialised documents, linking to |
paul@282 | 413 | separate objects instead. |
paul@186 | 414 | --output-dir Indicate an output directory to contain serialised document |
paul@186 | 415 | files |
paul@186 | 416 | --output-encoding Indicate the character encoding used in serialised document |
paul@186 | 417 | files |
paul@312 | 418 | --output-page-sep Indicate the separator used in filenames to encode |
paul@312 | 419 | hierarchical relationships (subpages and descendant pages) |
paul@186 | 420 | --theme Indicate a theme for serialised documents, typically |
paul@186 | 421 | requiring an output directory to be useful |
paul@186 | 422 | --tree Produce a document tree representation on standard output |
paul@186 | 423 | instead of generating output files |
paul@186 | 424 | |
paul@186 | 425 | Configuration options: |
paul@186 | 426 | |
paul@186 | 427 | --macros Perform macro evaluation/expansion before serialising |
paul@186 | 428 | documents |
paul@186 | 429 | --mapping Indicate a name and corresponding URL to be used to |
paul@186 | 430 | translate interwiki links |
paul@186 | 431 | --root Indicate the root page name to be used |
paul@186 | 432 | (default: FrontPage) |
paul@186 | 433 | """ |
paul@186 | 434 | |
paul@84 | 435 | if __name__ == "__main__": |
paul@209 | 436 | try: |
paul@209 | 437 | main() |
paul@209 | 438 | except errors.ProcessingError, exc: |
paul@209 | 439 | print str(exc) |
paul@84 | 440 | |
paul@84 | 441 | # vim: tabstop=4 expandtab shiftwidth=4 |