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