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