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