paul@84 | 1 | #!/usr/bin/env python |
paul@84 | 2 | |
paul@174 | 3 | """ |
paul@174 | 4 | Moin wiki format converter. |
paul@174 | 5 | |
paul@174 | 6 | Copyright (C) 2018 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@104 | 81 | # Option values. |
paul@104 | 82 | |
paul@84 | 83 | l = filenames = [] |
paul@84 | 84 | formats = [] |
paul@133 | 85 | input_dir_types = [] |
paul@133 | 86 | input_dirs = [] |
paul@104 | 87 | input_encodings = [] |
paul@133 | 88 | input_page_seps = [] |
paul@97 | 89 | mappings = [] |
paul@104 | 90 | output_dirs = [] |
paul@104 | 91 | output_encodings = [] |
paul@161 | 92 | theme_names = [] |
paul@104 | 93 | pagenames = [] |
paul@165 | 94 | root_pagenames = [] |
paul@104 | 95 | |
paul@104 | 96 | # Flags. |
paul@104 | 97 | |
paul@133 | 98 | all = False |
paul@161 | 99 | fragment = False |
paul@104 | 100 | macros = False |
paul@84 | 101 | tree = False |
paul@84 | 102 | |
paul@84 | 103 | for arg in args: |
paul@84 | 104 | |
paul@84 | 105 | # Detect tree output. |
paul@84 | 106 | |
paul@84 | 107 | if arg == "--tree": |
paul@84 | 108 | tree = True |
paul@84 | 109 | |
paul@89 | 110 | # Detect macro evaluation. |
paul@89 | 111 | |
paul@89 | 112 | elif arg == "--macros": |
paul@89 | 113 | macros = True |
paul@89 | 114 | |
paul@133 | 115 | # Detect all documents. |
paul@133 | 116 | |
paul@133 | 117 | elif arg == "--all": |
paul@133 | 118 | all = True |
paul@133 | 119 | |
paul@161 | 120 | # Detect fragment output (if serialising). |
paul@161 | 121 | |
paul@161 | 122 | elif arg == "--fragment": |
paul@161 | 123 | fragment = True |
paul@161 | 124 | |
paul@91 | 125 | # Switch to collecting formats. |
paul@84 | 126 | |
paul@84 | 127 | elif arg == "--format": |
paul@84 | 128 | l = formats |
paul@84 | 129 | continue |
paul@84 | 130 | |
paul@133 | 131 | # Switch to collecting input locations. |
paul@133 | 132 | |
paul@133 | 133 | elif arg == "--input-dir": |
paul@133 | 134 | l = input_dirs |
paul@133 | 135 | continue |
paul@133 | 136 | |
paul@133 | 137 | # Switch to collecting input context types. |
paul@133 | 138 | |
paul@133 | 139 | elif arg == "--input-dir-type": |
paul@133 | 140 | l = input_dir_types |
paul@133 | 141 | continue |
paul@133 | 142 | |
paul@104 | 143 | # Switch to collecting input encodings. |
paul@104 | 144 | |
paul@104 | 145 | elif arg == "--input-encoding": |
paul@104 | 146 | l = input_encodings |
paul@104 | 147 | continue |
paul@104 | 148 | |
paul@133 | 149 | # Switch to collecting input page hierarchy separators. |
paul@133 | 150 | |
paul@133 | 151 | elif arg == "--input-page-sep": |
paul@133 | 152 | l = input_page_seps |
paul@133 | 153 | continue |
paul@133 | 154 | |
paul@97 | 155 | # Switch to collecting mappings. |
paul@97 | 156 | |
paul@97 | 157 | elif arg == "--mapping": |
paul@97 | 158 | l = mappings |
paul@97 | 159 | continue |
paul@97 | 160 | |
paul@100 | 161 | # Switch to collecting output locations. |
paul@100 | 162 | |
paul@104 | 163 | elif arg == "--output-dir": |
paul@104 | 164 | l = output_dirs |
paul@104 | 165 | continue |
paul@104 | 166 | |
paul@104 | 167 | # Switch to collecting output encodings. |
paul@104 | 168 | |
paul@104 | 169 | elif arg == "--output-encoding": |
paul@104 | 170 | l = output_encodings |
paul@100 | 171 | continue |
paul@100 | 172 | |
paul@91 | 173 | # Switch to collecting page names. |
paul@91 | 174 | |
paul@91 | 175 | elif arg == "--pagename": |
paul@91 | 176 | l = pagenames |
paul@91 | 177 | continue |
paul@91 | 178 | |
paul@165 | 179 | # Switch to collecting root page names. |
paul@165 | 180 | |
paul@165 | 181 | elif arg == "--root": |
paul@165 | 182 | l = root_pagenames |
paul@165 | 183 | continue |
paul@165 | 184 | |
paul@161 | 185 | # Switch to collecting theme names. |
paul@161 | 186 | |
paul@161 | 187 | elif arg == "--theme": |
paul@161 | 188 | l = theme_names |
paul@161 | 189 | continue |
paul@161 | 190 | |
paul@84 | 191 | # Collect options and arguments. |
paul@84 | 192 | |
paul@84 | 193 | else: |
paul@84 | 194 | l.append(arg) |
paul@84 | 195 | |
paul@100 | 196 | # Collect multiple mappings. |
paul@100 | 197 | |
paul@97 | 198 | if l is mappings: |
paul@97 | 199 | continue |
paul@97 | 200 | |
paul@84 | 201 | # Collect filenames normally. |
paul@84 | 202 | |
paul@84 | 203 | l = filenames |
paul@84 | 204 | |
paul@91 | 205 | format = formats and formats[0] or "html" |
paul@133 | 206 | input_dir = getvalue(input_dirs) |
paul@133 | 207 | output_dir = getvalue(output_dirs) |
paul@104 | 208 | |
paul@165 | 209 | # Define metadata. |
paul@97 | 210 | |
paul@165 | 211 | metadata = Metadata({ |
paul@165 | 212 | "input_context" : input_dir and \ |
paul@165 | 213 | getvalue(input_dir_types, "directory") or \ |
paul@165 | 214 | "standalone", |
paul@165 | 215 | "input_encoding" : getvalue(input_encodings), |
paul@165 | 216 | "input_filename" : input_dir, |
paul@165 | 217 | "input_separator" : getvalue(input_page_seps), |
paul@165 | 218 | "link_format" : format, |
paul@165 | 219 | "mapping" : getmapping(mappings), |
paul@165 | 220 | "output_context" : output_dir and "directory" or "standalone", |
paul@165 | 221 | "output_encoding" : getvalue(output_encodings), |
paul@165 | 222 | "output_format" : format, |
paul@165 | 223 | "output_filename" : output_dir, |
paul@165 | 224 | "root_pagename" : getvalue(root_pagenames, "FrontPage"), |
paul@165 | 225 | "theme_name" : not fragment and \ |
paul@165 | 226 | "%s.%s" % (getvalue(theme_names, "default"), format) or None, |
paul@165 | 227 | }) |
paul@133 | 228 | |
paul@165 | 229 | # Define the input context and theme. |
paul@161 | 230 | |
paul@165 | 231 | input = metadata.get_input() |
paul@165 | 232 | theme = metadata.get_theme() |
paul@161 | 233 | |
paul@133 | 234 | # Treat filenames as pagenames if an input directory is indicated and if no |
paul@133 | 235 | # pagenames are explicitly specified. |
paul@104 | 236 | |
paul@133 | 237 | if input_dir: |
paul@133 | 238 | if pagenames: |
paul@174 | 239 | print >>sys.stderr, message_explicit_pagenames |
paul@133 | 240 | sys.exit(1) |
paul@104 | 241 | |
paul@133 | 242 | if all: |
paul@133 | 243 | if filenames: |
paul@174 | 244 | print >>sys.stderr, message_all_with_filenames |
paul@133 | 245 | sys.exit(1) |
paul@133 | 246 | else: |
paul@133 | 247 | filenames = input.all() |
paul@104 | 248 | |
paul@133 | 249 | pagenames = filenames |
paul@133 | 250 | filenames = [] |
paul@133 | 251 | |
paul@133 | 252 | # Open each file or page, parse the content, serialise the document. |
paul@104 | 253 | |
paul@133 | 254 | for pagename, filename in map(None, pagenames, filenames): |
paul@133 | 255 | |
paul@133 | 256 | # Define a pagename if missing. |
paul@133 | 257 | |
paul@133 | 258 | pagename = pagename or split(filename)[-1] |
paul@165 | 259 | metadata.set("pagename", pagename) |
paul@89 | 260 | |
paul@133 | 261 | # Read either from a filename or using a pagename. |
paul@133 | 262 | |
paul@133 | 263 | if filename: |
paul@133 | 264 | pagetext = input.readfile(filename) |
paul@133 | 265 | else: |
paul@133 | 266 | pagetext = input.readpage(pagename) |
paul@133 | 267 | |
paul@133 | 268 | # Parse the page content. |
paul@104 | 269 | |
paul@165 | 270 | p = make_parser(metadata) |
paul@133 | 271 | d = parse(pagetext, p) |
paul@133 | 272 | |
paul@133 | 273 | if macros: |
paul@133 | 274 | p.evaluate_macros() |
paul@104 | 275 | |
paul@133 | 276 | # Show a document tree for debugging purposes, if requested. |
paul@133 | 277 | |
paul@133 | 278 | if tree: |
paul@133 | 279 | print d.prettyprint() |
paul@133 | 280 | continue |
paul@133 | 281 | |
paul@133 | 282 | # Otherwise, serialise the document. |
paul@104 | 283 | |
paul@104 | 284 | # Obtain a serialiser using the configuration. |
paul@104 | 285 | |
paul@165 | 286 | serialiser = make_serialiser(metadata) |
paul@133 | 287 | outtext = serialise(d, serialiser) |
paul@133 | 288 | |
paul@161 | 289 | # With a theme, apply it to the text. |
paul@161 | 290 | |
paul@161 | 291 | if theme: |
paul@161 | 292 | outtext = theme.apply(outtext) |
paul@161 | 293 | |
paul@133 | 294 | # If reading from a file, show the result. Otherwise, write to the |
paul@133 | 295 | # output context. |
paul@133 | 296 | |
paul@165 | 297 | output = metadata.get_output() |
paul@165 | 298 | |
paul@133 | 299 | if not output.can_write(): |
paul@133 | 300 | print outtext |
paul@133 | 301 | else: |
paul@133 | 302 | output.writepage(outtext, pagename) |
paul@133 | 303 | print >>sys.stderr, pagename |
paul@84 | 304 | |
paul@161 | 305 | # Install any theme resources. |
paul@161 | 306 | |
paul@161 | 307 | if theme: |
paul@161 | 308 | theme.install_resources() |
paul@161 | 309 | |
paul@84 | 310 | if __name__ == "__main__": |
paul@84 | 311 | main() |
paul@84 | 312 | |
paul@84 | 313 | # vim: tabstop=4 expandtab shiftwidth=4 |