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