paul@84 | 1 | #!/usr/bin/env python |
paul@84 | 2 | |
paul@174 | 3 | """ |
paul@174 | 4 | Moin wiki format converter. |
paul@174 | 5 | |
paul@196 | 6 | Copyright (C) 2018, 2019 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@104 | 99 | pagenames = [] |
paul@165 | 100 | root_pagenames = [] |
paul@196 | 101 | theme_names = [] |
paul@196 | 102 | |
paul@196 | 103 | # Obtain filenames by default. |
paul@196 | 104 | |
paul@196 | 105 | l = filenames |
paul@104 | 106 | |
paul@104 | 107 | # Flags. |
paul@104 | 108 | |
paul@133 | 109 | all = False |
paul@161 | 110 | fragment = False |
paul@104 | 111 | macros = False |
paul@84 | 112 | tree = False |
paul@84 | 113 | |
paul@84 | 114 | for arg in args: |
paul@84 | 115 | |
paul@215 | 116 | # Flags with no following arguments. |
paul@215 | 117 | |
paul@215 | 118 | # Detect all documents. |
paul@84 | 119 | |
paul@215 | 120 | if arg == "--all": |
paul@215 | 121 | all = True |
paul@215 | 122 | |
paul@215 | 123 | # Detect fragment output (if serialising). |
paul@215 | 124 | |
paul@215 | 125 | elif arg == "--fragment": |
paul@215 | 126 | fragment = True |
paul@84 | 127 | |
paul@89 | 128 | # Detect macro evaluation. |
paul@89 | 129 | |
paul@89 | 130 | elif arg == "--macros": |
paul@89 | 131 | macros = True |
paul@89 | 132 | |
paul@215 | 133 | # Detect tree output. |
paul@215 | 134 | |
paul@215 | 135 | elif arg == "--tree": |
paul@215 | 136 | tree = True |
paul@133 | 137 | |
paul@215 | 138 | # Options with following arguments. |
paul@215 | 139 | |
paul@215 | 140 | # Switch to document index. |
paul@215 | 141 | |
paul@215 | 142 | elif arg == "--attachments-dir": |
paul@215 | 143 | l = attachments_dir |
paul@215 | 144 | continue |
paul@133 | 145 | |
paul@196 | 146 | # Switch to document index. |
paul@196 | 147 | |
paul@196 | 148 | elif arg == "--document-index": |
paul@196 | 149 | l = document_indexes |
paul@196 | 150 | continue |
paul@196 | 151 | |
paul@91 | 152 | # Switch to collecting formats. |
paul@84 | 153 | |
paul@84 | 154 | elif arg == "--format": |
paul@84 | 155 | l = formats |
paul@84 | 156 | continue |
paul@84 | 157 | |
paul@133 | 158 | # Switch to collecting input locations. |
paul@133 | 159 | |
paul@133 | 160 | elif arg == "--input-dir": |
paul@133 | 161 | l = input_dirs |
paul@133 | 162 | continue |
paul@133 | 163 | |
paul@133 | 164 | # Switch to collecting input context types. |
paul@133 | 165 | |
paul@133 | 166 | elif arg == "--input-dir-type": |
paul@133 | 167 | l = input_dir_types |
paul@133 | 168 | continue |
paul@133 | 169 | |
paul@104 | 170 | # Switch to collecting input encodings. |
paul@104 | 171 | |
paul@104 | 172 | elif arg == "--input-encoding": |
paul@104 | 173 | l = input_encodings |
paul@104 | 174 | continue |
paul@104 | 175 | |
paul@133 | 176 | # Switch to collecting input page hierarchy separators. |
paul@133 | 177 | |
paul@133 | 178 | elif arg == "--input-page-sep": |
paul@133 | 179 | l = input_page_seps |
paul@133 | 180 | continue |
paul@133 | 181 | |
paul@97 | 182 | # Switch to collecting mappings. |
paul@97 | 183 | |
paul@97 | 184 | elif arg == "--mapping": |
paul@97 | 185 | l = mappings |
paul@97 | 186 | continue |
paul@97 | 187 | |
paul@100 | 188 | # Switch to collecting output locations. |
paul@100 | 189 | |
paul@104 | 190 | elif arg == "--output-dir": |
paul@104 | 191 | l = output_dirs |
paul@104 | 192 | continue |
paul@104 | 193 | |
paul@104 | 194 | # Switch to collecting output encodings. |
paul@104 | 195 | |
paul@104 | 196 | elif arg == "--output-encoding": |
paul@104 | 197 | l = output_encodings |
paul@100 | 198 | continue |
paul@100 | 199 | |
paul@91 | 200 | # Switch to collecting page names. |
paul@91 | 201 | |
paul@91 | 202 | elif arg == "--pagename": |
paul@91 | 203 | l = pagenames |
paul@91 | 204 | continue |
paul@91 | 205 | |
paul@165 | 206 | # Switch to collecting root page names. |
paul@165 | 207 | |
paul@165 | 208 | elif arg == "--root": |
paul@165 | 209 | l = root_pagenames |
paul@165 | 210 | continue |
paul@165 | 211 | |
paul@161 | 212 | # Switch to collecting theme names. |
paul@161 | 213 | |
paul@161 | 214 | elif arg == "--theme": |
paul@161 | 215 | l = theme_names |
paul@161 | 216 | continue |
paul@161 | 217 | |
paul@84 | 218 | # Collect options and arguments. |
paul@84 | 219 | |
paul@84 | 220 | else: |
paul@84 | 221 | l.append(arg) |
paul@84 | 222 | |
paul@100 | 223 | # Collect multiple mappings. |
paul@100 | 224 | |
paul@97 | 225 | if l is mappings: |
paul@97 | 226 | continue |
paul@97 | 227 | |
paul@84 | 228 | # Collect filenames normally. |
paul@84 | 229 | |
paul@84 | 230 | l = filenames |
paul@84 | 231 | |
paul@91 | 232 | format = formats and formats[0] or "html" |
paul@133 | 233 | input_dir = getvalue(input_dirs) |
paul@133 | 234 | output_dir = getvalue(output_dirs) |
paul@104 | 235 | |
paul@165 | 236 | # Define metadata. |
paul@97 | 237 | |
paul@165 | 238 | metadata = Metadata({ |
paul@215 | 239 | "attachments" : getvalue(attachments_dir, "attachments"), |
paul@196 | 240 | "document_index" : getvalue(document_indexes), |
paul@165 | 241 | "input_context" : input_dir and \ |
paul@165 | 242 | getvalue(input_dir_types, "directory") or \ |
paul@165 | 243 | "standalone", |
paul@165 | 244 | "input_encoding" : getvalue(input_encodings), |
paul@165 | 245 | "input_filename" : input_dir, |
paul@165 | 246 | "input_separator" : getvalue(input_page_seps), |
paul@165 | 247 | "link_format" : format, |
paul@165 | 248 | "mapping" : getmapping(mappings), |
paul@165 | 249 | "output_context" : output_dir and "directory" or "standalone", |
paul@165 | 250 | "output_encoding" : getvalue(output_encodings), |
paul@165 | 251 | "output_format" : format, |
paul@165 | 252 | "output_filename" : output_dir, |
paul@165 | 253 | "root_pagename" : getvalue(root_pagenames, "FrontPage"), |
paul@165 | 254 | "theme_name" : not fragment and \ |
paul@165 | 255 | "%s.%s" % (getvalue(theme_names, "default"), format) or None, |
paul@165 | 256 | }) |
paul@133 | 257 | |
paul@165 | 258 | # Define the input context and theme. |
paul@161 | 259 | |
paul@165 | 260 | input = metadata.get_input() |
paul@165 | 261 | theme = metadata.get_theme() |
paul@161 | 262 | |
paul@133 | 263 | # Treat filenames as pagenames if an input directory is indicated and if no |
paul@133 | 264 | # pagenames are explicitly specified. |
paul@104 | 265 | |
paul@133 | 266 | if input_dir: |
paul@133 | 267 | if pagenames: |
paul@174 | 268 | print >>sys.stderr, message_explicit_pagenames |
paul@133 | 269 | sys.exit(1) |
paul@104 | 270 | |
paul@133 | 271 | if all: |
paul@133 | 272 | if filenames: |
paul@174 | 273 | print >>sys.stderr, message_all_with_filenames |
paul@133 | 274 | sys.exit(1) |
paul@133 | 275 | else: |
paul@133 | 276 | filenames = input.all() |
paul@104 | 277 | |
paul@133 | 278 | pagenames = filenames |
paul@133 | 279 | filenames = [] |
paul@133 | 280 | |
paul@133 | 281 | # Open each file or page, parse the content, serialise the document. |
paul@104 | 282 | |
paul@133 | 283 | for pagename, filename in map(None, pagenames, filenames): |
paul@133 | 284 | |
paul@133 | 285 | # Define a pagename if missing. |
paul@133 | 286 | |
paul@133 | 287 | pagename = pagename or split(filename)[-1] |
paul@165 | 288 | metadata.set("pagename", pagename) |
paul@89 | 289 | |
paul@133 | 290 | # Read either from a filename or using a pagename. |
paul@133 | 291 | |
paul@133 | 292 | if filename: |
paul@133 | 293 | pagetext = input.readfile(filename) |
paul@133 | 294 | else: |
paul@133 | 295 | pagetext = input.readpage(pagename) |
paul@133 | 296 | |
paul@133 | 297 | # Parse the page content. |
paul@104 | 298 | |
paul@165 | 299 | p = make_parser(metadata) |
paul@133 | 300 | d = parse(pagetext, p) |
paul@133 | 301 | |
paul@133 | 302 | if macros: |
paul@133 | 303 | p.evaluate_macros() |
paul@104 | 304 | |
paul@199 | 305 | p.update_metadata(metadata) |
paul@199 | 306 | |
paul@133 | 307 | # Show a document tree for debugging purposes, if requested. |
paul@133 | 308 | |
paul@133 | 309 | if tree: |
paul@133 | 310 | print d.prettyprint() |
paul@133 | 311 | continue |
paul@133 | 312 | |
paul@133 | 313 | # Otherwise, serialise the document. |
paul@104 | 314 | |
paul@104 | 315 | # Obtain a serialiser using the configuration. |
paul@104 | 316 | |
paul@165 | 317 | serialiser = make_serialiser(metadata) |
paul@133 | 318 | outtext = serialise(d, serialiser) |
paul@133 | 319 | |
paul@161 | 320 | # With a theme, apply it to the text. |
paul@161 | 321 | |
paul@161 | 322 | if theme: |
paul@161 | 323 | outtext = theme.apply(outtext) |
paul@161 | 324 | |
paul@133 | 325 | # If reading from a file, show the result. Otherwise, write to the |
paul@133 | 326 | # output context. |
paul@133 | 327 | |
paul@165 | 328 | output = metadata.get_output() |
paul@165 | 329 | |
paul@133 | 330 | if not output.can_write(): |
paul@133 | 331 | print outtext |
paul@133 | 332 | else: |
paul@133 | 333 | output.writepage(outtext, pagename) |
paul@133 | 334 | print >>sys.stderr, pagename |
paul@84 | 335 | |
paul@223 | 336 | copy_attachments(p, input, output) |
paul@223 | 337 | |
paul@161 | 338 | # Install any theme resources. |
paul@161 | 339 | |
paul@161 | 340 | if theme: |
paul@161 | 341 | theme.install_resources() |
paul@161 | 342 | |
paul@186 | 343 | def show_help(progname): |
paul@186 | 344 | |
paul@186 | 345 | "Show the help text." |
paul@186 | 346 | |
paul@186 | 347 | print >>sys.stderr, help_text % progname |
paul@186 | 348 | |
paul@186 | 349 | help_text = """\ |
paul@186 | 350 | Usage: %s [ <options> ] ( --all | <filename>... ) |
paul@186 | 351 | |
paul@198 | 352 | Input file options: |
paul@198 | 353 | |
paul@198 | 354 | --all Detect all document files in the specified input directory |
paul@198 | 355 | |
paul@186 | 356 | Input options: |
paul@186 | 357 | |
paul@186 | 358 | --input-dir Indicate an input directory containing document files |
paul@186 | 359 | --input-dir-type Indicate the type of input directory involved |
paul@186 | 360 | (default: directory) |
paul@186 | 361 | --input-encoding Indicate the character encoding used in document files |
paul@186 | 362 | --input-page-sep Indicate the separator used in filenames to encode |
paul@186 | 363 | hierarchical relationships (subpages and descendant pages) |
paul@186 | 364 | --pagename Indicate the page name corresponding to an indicated |
paul@186 | 365 | filename, with each successive instance of this option |
paul@186 | 366 | corresponding to each successive filename instance |
paul@186 | 367 | |
paul@186 | 368 | Output options: |
paul@186 | 369 | |
paul@196 | 370 | --document-index Provide a "DocumentIndex" filename to be used in links in |
paul@196 | 371 | HTML format output, useful for local file browsing instead |
paul@196 | 372 | of Web-published content |
paul@186 | 373 | --format Indicate the format to be used for serialised documents |
paul@186 | 374 | (default: html) |
paul@186 | 375 | --fragment Indicates that an output fragment, not an entire document, |
paul@186 | 376 | is to be generated, skipping any theming activities |
paul@186 | 377 | --output-dir Indicate an output directory to contain serialised document |
paul@186 | 378 | files |
paul@186 | 379 | --output-encoding Indicate the character encoding used in serialised document |
paul@186 | 380 | files |
paul@186 | 381 | --theme Indicate a theme for serialised documents, typically |
paul@186 | 382 | requiring an output directory to be useful |
paul@186 | 383 | --tree Produce a document tree representation on standard output |
paul@186 | 384 | instead of generating output files |
paul@186 | 385 | |
paul@186 | 386 | Configuration options: |
paul@186 | 387 | |
paul@186 | 388 | --macros Perform macro evaluation/expansion before serialising |
paul@186 | 389 | documents |
paul@186 | 390 | --mapping Indicate a name and corresponding URL to be used to |
paul@186 | 391 | translate interwiki links |
paul@186 | 392 | --root Indicate the root page name to be used |
paul@186 | 393 | (default: FrontPage) |
paul@186 | 394 | """ |
paul@186 | 395 | |
paul@84 | 396 | if __name__ == "__main__": |
paul@209 | 397 | try: |
paul@209 | 398 | main() |
paul@209 | 399 | except errors.ProcessingError, exc: |
paul@209 | 400 | print str(exc) |
paul@84 | 401 | |
paul@84 | 402 | # vim: tabstop=4 expandtab shiftwidth=4 |