1 #!/usr/bin/env python 2 3 from moinformat import make_input, make_linker, make_output, make_parser, \ 4 make_serialiser, make_theme, parse, serialise 5 from os.path import split 6 import sys 7 8 def getmapping(mappings): 9 mapping = {} 10 key = None 11 12 for arg in mappings: 13 if key is None: 14 key = arg 15 else: 16 mapping[key] = arg 17 key = None 18 19 return mapping 20 21 def getvalue(values): 22 return values and values[0] or None 23 24 def main(): 25 dirname, progname = split(sys.argv[0]) 26 args = sys.argv[1:] 27 28 # Option values. 29 30 l = filenames = [] 31 formats = [] 32 input_dir_types = [] 33 input_dirs = [] 34 input_encodings = [] 35 input_page_seps = [] 36 mappings = [] 37 output_dirs = [] 38 output_encodings = [] 39 theme_names = [] 40 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 theme names. 126 127 elif arg == "--theme": 128 l = theme_names 129 continue 130 131 # Collect options and arguments. 132 133 else: 134 l.append(arg) 135 136 # Collect multiple mappings. 137 138 if l is mappings: 139 continue 140 141 # Collect filenames normally. 142 143 l = filenames 144 145 format = formats and formats[0] or "html" 146 147 # Derive a proper mapping from the given list of values. 148 149 mapping = getmapping(mappings) 150 151 # Obtain encodings. 152 153 input_encoding = getvalue(input_encodings) 154 output_encoding = getvalue(output_encodings) 155 156 # Obtain the input and output locations and contexts. 157 158 input_dir = getvalue(input_dirs) 159 output_dir = getvalue(output_dirs) 160 161 input_page_sep = getvalue(input_page_seps) 162 163 input_context = input_dir and (getvalue(input_dir_types) or 164 "directory") or "standalone" 165 166 input = make_input(input_context, {"encoding" : input_encoding, 167 "filename" : input_dir, 168 "separator" : input_page_sep}) 169 170 output_context = output_dir and "directory" or "standalone" 171 172 output = make_output(output_context, {"encoding" : output_encoding, 173 "filename" : output_dir}) 174 175 # Obtain a theme name. 176 177 theme_name = not fragment and (getvalue(theme_names) or "default") or None 178 179 theme = None 180 181 # Treat filenames as pagenames if an input directory is indicated and if no 182 # pagenames are explicitly specified. 183 184 if input_dir: 185 if pagenames: 186 print >>sys.stderr, """\ 187 Explicit pagenames (indicated using --pagename) are only to be specified when 188 providing filenames without an input directory (indicated using --input-dir). 189 190 To indicate pagenames within an input directory, omit any --pagename flags.""" 191 sys.exit(1) 192 193 if all: 194 if filenames: 195 print >>sys.stderr, """\ 196 Using --all overrides any indicated pagenames. Either --all or the filenames 197 should be omitted.""" 198 sys.exit(1) 199 else: 200 filenames = input.all() 201 202 pagenames = filenames 203 filenames = [] 204 205 # Open each file or page, parse the content, serialise the document. 206 207 for pagename, filename in map(None, pagenames, filenames): 208 209 # Define a pagename if missing. 210 211 pagename = pagename or split(filename)[-1] 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() 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 linker using format and pagename details. 237 238 linker = make_linker(format, pagename, mapping) 239 240 # Obtain a serialiser using the configuration. 241 242 serialiser = make_serialiser(format, output, linker, pagename) 243 outtext = serialise(d, serialiser) 244 245 # Obtain a theme object for theming. 246 247 theme = theme_name and make_theme("%s.%s" % (theme_name, format), 248 output, linker, pagename) 249 250 # With a theme, apply it to the text. 251 252 if theme: 253 outtext = theme.apply(outtext) 254 255 # If reading from a file, show the result. Otherwise, write to the 256 # output context. 257 258 if not output.can_write(): 259 print outtext 260 else: 261 output.writepage(outtext, pagename) 262 print >>sys.stderr, pagename 263 264 # Install any theme resources. 265 266 if theme: 267 theme.install_resources() 268 269 if __name__ == "__main__": 270 main() 271 272 # vim: tabstop=4 expandtab shiftwidth=4