1 #!/usr/bin/env python 2 3 from moinformat import make_input, make_linker, make_output, make_parser, \ 4 make_serialiser, 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 pagenames = [] 40 41 # Flags. 42 43 all = False 44 macros = False 45 tree = False 46 47 for arg in args: 48 49 # Detect tree output. 50 51 if arg == "--tree": 52 tree = True 53 54 # Detect macro evaluation. 55 56 elif arg == "--macros": 57 macros = True 58 59 # Detect all documents. 60 61 elif arg == "--all": 62 all = True 63 64 # Switch to collecting formats. 65 66 elif arg == "--format": 67 l = formats 68 continue 69 70 # Switch to collecting input locations. 71 72 elif arg == "--input-dir": 73 l = input_dirs 74 continue 75 76 # Switch to collecting input context types. 77 78 elif arg == "--input-dir-type": 79 l = input_dir_types 80 continue 81 82 # Switch to collecting input encodings. 83 84 elif arg == "--input-encoding": 85 l = input_encodings 86 continue 87 88 # Switch to collecting input page hierarchy separators. 89 90 elif arg == "--input-page-sep": 91 l = input_page_seps 92 continue 93 94 # Switch to collecting mappings. 95 96 elif arg == "--mapping": 97 l = mappings 98 continue 99 100 # Switch to collecting output locations. 101 102 elif arg == "--output-dir": 103 l = output_dirs 104 continue 105 106 # Switch to collecting output encodings. 107 108 elif arg == "--output-encoding": 109 l = output_encodings 110 continue 111 112 # Switch to collecting page names. 113 114 elif arg == "--pagename": 115 l = pagenames 116 continue 117 118 # Collect options and arguments. 119 120 else: 121 l.append(arg) 122 123 # Collect multiple mappings. 124 125 if l is mappings: 126 continue 127 128 # Collect filenames normally. 129 130 l = filenames 131 132 format = formats and formats[0] or "html" 133 134 # Derive a proper mapping from the given list of values. 135 136 mapping = getmapping(mappings) 137 138 # Obtain encodings. 139 140 input_encoding = getvalue(input_encodings) 141 output_encoding = getvalue(output_encodings) 142 143 # Obtain the input and output locations. 144 145 input_dir = getvalue(input_dirs) 146 output_dir = getvalue(output_dirs) 147 148 input_page_sep = getvalue(input_page_seps) 149 150 input_context = input_dir and (getvalue(input_dir_types) or 151 "directory") or "standalone" 152 153 input = make_input(input_context, {"encoding" : input_encoding, 154 "filename" : input_dir, 155 "separator" : input_page_sep}) 156 157 output_context = output_dir and "directory" or "standalone" 158 159 output = make_output(output_context, {"encoding" : output_encoding, 160 "filename" : output_dir}) 161 162 # Treat filenames as pagenames if an input directory is indicated and if no 163 # pagenames are explicitly specified. 164 165 if input_dir: 166 if pagenames: 167 print >>sys.stderr, """\ 168 Explicit pagenames (indicated using --pagename) are only to be specified when 169 providing filenames without an input directory (indicated using --input-dir). 170 171 To indicate pagenames within an input directory, omit any --pagename flags.""" 172 sys.exit(1) 173 174 if all: 175 if filenames: 176 print >>sys.stderr, """\ 177 Using --all overrides any indicated pagenames. Either --all or the filenames 178 should be omitted.""" 179 sys.exit(1) 180 else: 181 filenames = input.all() 182 183 pagenames = filenames 184 filenames = [] 185 186 # Open each file or page, parse the content, serialise the document. 187 188 for pagename, filename in map(None, pagenames, filenames): 189 190 # Define a pagename if missing. 191 192 pagename = pagename or split(filename)[-1] 193 194 # Read either from a filename or using a pagename. 195 196 if filename: 197 pagetext = input.readfile(filename) 198 else: 199 pagetext = input.readpage(pagename) 200 201 # Parse the page content. 202 203 p = make_parser() 204 d = parse(pagetext, p) 205 206 if macros: 207 p.evaluate_macros() 208 209 # Show a document tree for debugging purposes, if requested. 210 211 if tree: 212 print d.prettyprint() 213 continue 214 215 # Otherwise, serialise the document. 216 217 # Obtain a linker using format and pagename details. 218 219 linker = make_linker(format, pagename, mapping) 220 221 # Obtain a serialiser using the configuration. 222 223 serialiser = make_serialiser(format, output, linker) 224 outtext = serialise(d, serialiser) 225 226 # If reading from a file, show the result. Otherwise, write to the 227 # output context. 228 229 if not output.can_write(): 230 print outtext 231 else: 232 output.writepage(outtext, pagename) 233 print >>sys.stderr, pagename 234 235 if __name__ == "__main__": 236 main() 237 238 # vim: tabstop=4 expandtab shiftwidth=4