paul@84 | 1 | #!/usr/bin/env python |
paul@84 | 2 | |
paul@104 | 3 | from moinformat import make_input, make_linker, make_output, make_parser, \ |
paul@161 | 4 | make_serialiser, make_theme, parse, serialise |
paul@84 | 5 | from os.path import split |
paul@84 | 6 | import sys |
paul@84 | 7 | |
paul@133 | 8 | def getmapping(mappings): |
paul@133 | 9 | mapping = {} |
paul@133 | 10 | key = None |
paul@133 | 11 | |
paul@133 | 12 | for arg in mappings: |
paul@133 | 13 | if key is None: |
paul@133 | 14 | key = arg |
paul@133 | 15 | else: |
paul@133 | 16 | mapping[key] = arg |
paul@133 | 17 | key = None |
paul@133 | 18 | |
paul@133 | 19 | return mapping |
paul@133 | 20 | |
paul@133 | 21 | def getvalue(values): |
paul@133 | 22 | return values and values[0] or None |
paul@133 | 23 | |
paul@84 | 24 | def main(): |
paul@84 | 25 | dirname, progname = split(sys.argv[0]) |
paul@84 | 26 | args = sys.argv[1:] |
paul@84 | 27 | |
paul@104 | 28 | # Option values. |
paul@104 | 29 | |
paul@84 | 30 | l = filenames = [] |
paul@84 | 31 | formats = [] |
paul@133 | 32 | input_dir_types = [] |
paul@133 | 33 | input_dirs = [] |
paul@104 | 34 | input_encodings = [] |
paul@133 | 35 | input_page_seps = [] |
paul@97 | 36 | mappings = [] |
paul@104 | 37 | output_dirs = [] |
paul@104 | 38 | output_encodings = [] |
paul@161 | 39 | theme_names = [] |
paul@104 | 40 | pagenames = [] |
paul@104 | 41 | |
paul@104 | 42 | # Flags. |
paul@104 | 43 | |
paul@133 | 44 | all = False |
paul@161 | 45 | fragment = False |
paul@104 | 46 | macros = False |
paul@84 | 47 | tree = False |
paul@84 | 48 | |
paul@84 | 49 | for arg in args: |
paul@84 | 50 | |
paul@84 | 51 | # Detect tree output. |
paul@84 | 52 | |
paul@84 | 53 | if arg == "--tree": |
paul@84 | 54 | tree = True |
paul@84 | 55 | |
paul@89 | 56 | # Detect macro evaluation. |
paul@89 | 57 | |
paul@89 | 58 | elif arg == "--macros": |
paul@89 | 59 | macros = True |
paul@89 | 60 | |
paul@133 | 61 | # Detect all documents. |
paul@133 | 62 | |
paul@133 | 63 | elif arg == "--all": |
paul@133 | 64 | all = True |
paul@133 | 65 | |
paul@161 | 66 | # Detect fragment output (if serialising). |
paul@161 | 67 | |
paul@161 | 68 | elif arg == "--fragment": |
paul@161 | 69 | fragment = True |
paul@161 | 70 | |
paul@91 | 71 | # Switch to collecting formats. |
paul@84 | 72 | |
paul@84 | 73 | elif arg == "--format": |
paul@84 | 74 | l = formats |
paul@84 | 75 | continue |
paul@84 | 76 | |
paul@133 | 77 | # Switch to collecting input locations. |
paul@133 | 78 | |
paul@133 | 79 | elif arg == "--input-dir": |
paul@133 | 80 | l = input_dirs |
paul@133 | 81 | continue |
paul@133 | 82 | |
paul@133 | 83 | # Switch to collecting input context types. |
paul@133 | 84 | |
paul@133 | 85 | elif arg == "--input-dir-type": |
paul@133 | 86 | l = input_dir_types |
paul@133 | 87 | continue |
paul@133 | 88 | |
paul@104 | 89 | # Switch to collecting input encodings. |
paul@104 | 90 | |
paul@104 | 91 | elif arg == "--input-encoding": |
paul@104 | 92 | l = input_encodings |
paul@104 | 93 | continue |
paul@104 | 94 | |
paul@133 | 95 | # Switch to collecting input page hierarchy separators. |
paul@133 | 96 | |
paul@133 | 97 | elif arg == "--input-page-sep": |
paul@133 | 98 | l = input_page_seps |
paul@133 | 99 | continue |
paul@133 | 100 | |
paul@97 | 101 | # Switch to collecting mappings. |
paul@97 | 102 | |
paul@97 | 103 | elif arg == "--mapping": |
paul@97 | 104 | l = mappings |
paul@97 | 105 | continue |
paul@97 | 106 | |
paul@100 | 107 | # Switch to collecting output locations. |
paul@100 | 108 | |
paul@104 | 109 | elif arg == "--output-dir": |
paul@104 | 110 | l = output_dirs |
paul@104 | 111 | continue |
paul@104 | 112 | |
paul@104 | 113 | # Switch to collecting output encodings. |
paul@104 | 114 | |
paul@104 | 115 | elif arg == "--output-encoding": |
paul@104 | 116 | l = output_encodings |
paul@100 | 117 | continue |
paul@100 | 118 | |
paul@91 | 119 | # Switch to collecting page names. |
paul@91 | 120 | |
paul@91 | 121 | elif arg == "--pagename": |
paul@91 | 122 | l = pagenames |
paul@91 | 123 | continue |
paul@91 | 124 | |
paul@161 | 125 | # Switch to collecting theme names. |
paul@161 | 126 | |
paul@161 | 127 | elif arg == "--theme": |
paul@161 | 128 | l = theme_names |
paul@161 | 129 | continue |
paul@161 | 130 | |
paul@84 | 131 | # Collect options and arguments. |
paul@84 | 132 | |
paul@84 | 133 | else: |
paul@84 | 134 | l.append(arg) |
paul@84 | 135 | |
paul@100 | 136 | # Collect multiple mappings. |
paul@100 | 137 | |
paul@97 | 138 | if l is mappings: |
paul@97 | 139 | continue |
paul@97 | 140 | |
paul@84 | 141 | # Collect filenames normally. |
paul@84 | 142 | |
paul@84 | 143 | l = filenames |
paul@84 | 144 | |
paul@91 | 145 | format = formats and formats[0] or "html" |
paul@91 | 146 | |
paul@97 | 147 | # Derive a proper mapping from the given list of values. |
paul@97 | 148 | |
paul@133 | 149 | mapping = getmapping(mappings) |
paul@104 | 150 | |
paul@104 | 151 | # Obtain encodings. |
paul@104 | 152 | |
paul@133 | 153 | input_encoding = getvalue(input_encodings) |
paul@133 | 154 | output_encoding = getvalue(output_encodings) |
paul@133 | 155 | |
paul@161 | 156 | # Obtain the input and output locations and contexts. |
paul@133 | 157 | |
paul@133 | 158 | input_dir = getvalue(input_dirs) |
paul@133 | 159 | output_dir = getvalue(output_dirs) |
paul@104 | 160 | |
paul@133 | 161 | input_page_sep = getvalue(input_page_seps) |
paul@133 | 162 | |
paul@133 | 163 | input_context = input_dir and (getvalue(input_dir_types) or |
paul@133 | 164 | "directory") or "standalone" |
paul@133 | 165 | |
paul@133 | 166 | input = make_input(input_context, {"encoding" : input_encoding, |
paul@133 | 167 | "filename" : input_dir, |
paul@133 | 168 | "separator" : input_page_sep}) |
paul@97 | 169 | |
paul@133 | 170 | output_context = output_dir and "directory" or "standalone" |
paul@133 | 171 | |
paul@133 | 172 | output = make_output(output_context, {"encoding" : output_encoding, |
paul@133 | 173 | "filename" : output_dir}) |
paul@133 | 174 | |
paul@161 | 175 | # Obtain a theme name. |
paul@161 | 176 | |
paul@161 | 177 | theme_name = not fragment and (getvalue(theme_names) or "default") or None |
paul@161 | 178 | |
paul@161 | 179 | theme = None |
paul@161 | 180 | |
paul@133 | 181 | # Treat filenames as pagenames if an input directory is indicated and if no |
paul@133 | 182 | # pagenames are explicitly specified. |
paul@104 | 183 | |
paul@133 | 184 | if input_dir: |
paul@133 | 185 | if pagenames: |
paul@133 | 186 | print >>sys.stderr, """\ |
paul@133 | 187 | Explicit pagenames (indicated using --pagename) are only to be specified when |
paul@133 | 188 | providing filenames without an input directory (indicated using --input-dir). |
paul@89 | 189 | |
paul@133 | 190 | To indicate pagenames within an input directory, omit any --pagename flags.""" |
paul@133 | 191 | sys.exit(1) |
paul@104 | 192 | |
paul@133 | 193 | if all: |
paul@133 | 194 | if filenames: |
paul@133 | 195 | print >>sys.stderr, """\ |
paul@133 | 196 | Using --all overrides any indicated pagenames. Either --all or the filenames |
paul@133 | 197 | should be omitted.""" |
paul@133 | 198 | sys.exit(1) |
paul@133 | 199 | else: |
paul@133 | 200 | filenames = input.all() |
paul@104 | 201 | |
paul@133 | 202 | pagenames = filenames |
paul@133 | 203 | filenames = [] |
paul@133 | 204 | |
paul@133 | 205 | # Open each file or page, parse the content, serialise the document. |
paul@104 | 206 | |
paul@133 | 207 | for pagename, filename in map(None, pagenames, filenames): |
paul@133 | 208 | |
paul@133 | 209 | # Define a pagename if missing. |
paul@133 | 210 | |
paul@133 | 211 | pagename = pagename or split(filename)[-1] |
paul@89 | 212 | |
paul@133 | 213 | # Read either from a filename or using a pagename. |
paul@133 | 214 | |
paul@133 | 215 | if filename: |
paul@133 | 216 | pagetext = input.readfile(filename) |
paul@133 | 217 | else: |
paul@133 | 218 | pagetext = input.readpage(pagename) |
paul@133 | 219 | |
paul@133 | 220 | # Parse the page content. |
paul@104 | 221 | |
paul@133 | 222 | p = make_parser() |
paul@133 | 223 | d = parse(pagetext, p) |
paul@133 | 224 | |
paul@133 | 225 | if macros: |
paul@133 | 226 | p.evaluate_macros() |
paul@104 | 227 | |
paul@133 | 228 | # Show a document tree for debugging purposes, if requested. |
paul@133 | 229 | |
paul@133 | 230 | if tree: |
paul@133 | 231 | print d.prettyprint() |
paul@133 | 232 | continue |
paul@133 | 233 | |
paul@133 | 234 | # Otherwise, serialise the document. |
paul@104 | 235 | |
paul@104 | 236 | # Obtain a linker using format and pagename details. |
paul@104 | 237 | |
paul@104 | 238 | linker = make_linker(format, pagename, mapping) |
paul@104 | 239 | |
paul@104 | 240 | # Obtain a serialiser using the configuration. |
paul@104 | 241 | |
paul@144 | 242 | serialiser = make_serialiser(format, output, linker, pagename) |
paul@133 | 243 | outtext = serialise(d, serialiser) |
paul@133 | 244 | |
paul@161 | 245 | # Obtain a theme object for theming. |
paul@161 | 246 | |
paul@161 | 247 | theme = theme_name and make_theme("%s.%s" % (theme_name, format), |
paul@161 | 248 | output, linker, pagename) |
paul@161 | 249 | |
paul@161 | 250 | # With a theme, apply it to the text. |
paul@161 | 251 | |
paul@161 | 252 | if theme: |
paul@161 | 253 | outtext = theme.apply(outtext) |
paul@161 | 254 | |
paul@133 | 255 | # If reading from a file, show the result. Otherwise, write to the |
paul@133 | 256 | # output context. |
paul@133 | 257 | |
paul@133 | 258 | if not output.can_write(): |
paul@133 | 259 | print outtext |
paul@133 | 260 | else: |
paul@133 | 261 | output.writepage(outtext, pagename) |
paul@133 | 262 | print >>sys.stderr, pagename |
paul@84 | 263 | |
paul@161 | 264 | # Install any theme resources. |
paul@161 | 265 | |
paul@161 | 266 | if theme: |
paul@161 | 267 | theme.install_resources() |
paul@161 | 268 | |
paul@84 | 269 | if __name__ == "__main__": |
paul@84 | 270 | main() |
paul@84 | 271 | |
paul@84 | 272 | # vim: tabstop=4 expandtab shiftwidth=4 |