def add(self, pieces): "Add a new set of alternative pieces." alternative = Declaration('#' + unicode(len(self.alternatives))) alternative.pieces = pieces self.alternatives.append(alternative) Trace.debug('Alternatives: ' + unicode(self)) return self
def convert(self, image): "Convert an image to PNG" if not ImageConverter.active or Options.noconvert: return if image.origin.path == image.destination.path: return if image.destination.exists(): if image.origin.getmtime() <= image.destination.getmtime(): # file has not changed; do not convert return image.destination.createdirs() if Options.copyimages: Trace.debug('Copying ' + image.origin.path + ' to ' + image.destination.path) shutil.copy2(image.origin.path, image.destination.path) return converter, command = self.buildcommand(image) try: Trace.debug(converter + ' command: "' + command + '"') result = os.system(command.encode(sys.getfilesystemencoding())) if result != 0: Trace.error(converter + ' not installed; images will not be processed') ImageConverter.active = False return Trace.message('Converted ' + unicode(image.origin) + ' to ' + unicode(image.destination)) except OSError, exception: Trace.error('Error while converting image ' + unicode(image.origin) + ': ' + unicode(exception))
def add(self, pieces): "Add a new set of alternative pieces." alternative = Declaration('#' + str(len(self.alternatives))) alternative.pieces = pieces self.alternatives.append(alternative) Trace.debug('Alternatives: ' + str(self)) return self
def convert(self, image): "Convert an image to PNG" if not ImageConverter.active or Options.noconvert: return if image.origin.path == image.destination.path: return if image.destination.exists(): if image.origin.getmtime() <= image.destination.getmtime(): # file has not changed; do not convert return image.destination.createdirs() if Options.copyimages: Trace.debug('Copying ' + image.origin.path + ' to ' + image.destination.path) shutil.copy2(image.origin.path, image.destination.path) return converter, command = self.buildcommand(image) try: Trace.debug(converter + ' command: "' + command + '"') result = os.system(command.encode(sys.getfilesystemencoding())) if result != 0: Trace.error(converter + ' not installed; images will not be processed') ImageConverter.active = False return Trace.message('Converted ' + str(image.origin) + ' to ' + str(image.destination)) except OSError as exception: Trace.error('Error while converting image ' + str(image.origin) + ': ' + str(exception))
def addconstant(self, constant): "Add a constant value." if constant in Grammar.instance.constants: Trace.error('Repeated constant ' + constant) return Trace.debug('New constant: ' + constant) self.pieces.append(ConstantWord(constant))
def process(self): "Process the grammar and create all necessary structures." for key in JavaToPyConfig.declarations: self.variables[key] = JavaToPyConfig.declarations[key] for key in self.variables: self.declarations[key] = Declaration(key) for key in self.variables: Trace.debug('Interpreting ' + self.variables[key]) pos = TextPosition(self.variables[key]) self.declarations[key].parse(pos)
def parsebit(self, pos): "Parse the function that defines the macro." self.output = EmptyOutput() self.parameternumber = 0 self.defaults = [] self.factory.defining = True self.parseparameters(pos) self.factory.defining = False Trace.debug('New command ' + self.newcommand + ' (' + \ str(self.parameternumber) + ' parameters)') self.macros[self.newcommand] = self
def parsebit(self, pos): "Parse the function that defines the macro." self.output = EmptyOutput() self.parameternumber = 0 self.defaults = [] self.factory.defining = True self.parseparameters(pos) self.factory.defining = False Trace.debug('New command ' + self.newcommand + ' (' + \ unicode(self.parameternumber) + ' parameters)') self.macros[self.newcommand] = self
def parsevariable(self, pos): "Parse a variable." if pos.checkskip('$'): self.pieces.append(IdentifierWord()) return name = '$' + pos.globidentifier() if not name in Grammar.instance.declarations: Trace.error('Unknown variable ' + name) return Trace.debug('New variable ' + name) self.pieces.append(Grammar.instance.declarations[name])
def processinclude(self): "Process a regular include: standard child document." self.contents = [] olddir = Options.directory newdir = os.path.dirname(self.getparameter('filename')) if newdir != '': Trace.debug('Child dir: ' + newdir) Options.directory = os.path.join(Options.directory, newdir) try: self.convertinclude() finally: Options.directory = olddir
def parse(self, pos): "Parse the bracket." pos.pushending(']') declaration = Declaration('bracket').parse(pos) pos.popending(']') quantifier = pos.skipcurrent() if not quantifier in self.quantified: Trace.error('Unknown quantifier ' + quantifier) return self bracket = Cloner.clone(self.quantified[quantifier]).create(declaration, quantifier) Trace.debug('Bracket: ' + str(bracket)) return bracket
def parse(self, pos): "Parse the bracket." pos.pushending(']') declaration = Declaration('bracket').parse(pos) pos.popending(']') quantifier = pos.skipcurrent() if not quantifier in self.quantified: Trace.error('Unknown quantifier ' + quantifier) return self bracket = Cloner.clone(self.quantified[quantifier]).create( declaration, quantifier) Trace.debug('Bracket: ' + unicode(bracket)) return bracket
def process(self): "Include the provided child document" self.filename = os.path.join(Options.directory, self.getparameter('filename')) Trace.debug('Child document: ' + self.filename) LstParser().parsecontainer(self) command = self.getparameter('LatexCommand') if command == 'verbatiminput': self.readverbatim() return elif command == 'lstinputlisting': self.readlisting() return self.processinclude()
def match(self, tok): "Match the declaration against a tokenizer." decl = Declaration(self.key) state = tok.mark() for piece in self.pieces: Trace.debug('Matching ' + tok.current() + ' against ' + str(piece)) result = piece.match(tok) if not result: Trace.error('Mismatch of ' + tok.current() + ' against ' + str(piece)) tok.revert(state) return None decl.pieces.append(result) return decl
def nextstatement(self, tok): "Return the next statement." statement = None while not statement and not tok.finished(): indent = self.chooser.getindent(tok) statement = self.parsestatement(tok) if not statement: return '' Trace.debug('Statement: ' + statement.strip()) if statement.startswith('\n'): # displace newline return '\n' + indent + statement[1:] return indent + statement
def match(self, tok): "Match the declaration against a tokenizer." decl = Declaration(self.key) state = tok.mark() for piece in self.pieces: Trace.debug('Matching ' + tok.current() + ' against ' + unicode(piece)) result = piece.match(tok) if not result: Trace.error('Mismatch of ' + tok.current() + ' against ' + unicode(piece)) tok.revert(state) return None decl.pieces.append(result) return decl
def parseentry(self, pos): "Parse a single entry" for entry in BibEntry.instances: if entry.detect(pos): newentry = Cloner.clone(entry) newentry.parse(pos) if not newentry.isvisible(): return if self.showall or newentry.isreferenced(): self.entries.append(newentry) self.added += 1 else: Trace.debug('Ignored entry ' + unicode(newentry)) self.ignored += 1 return # Skip the whole line since it's a comment outside an entry pos.globincluding('\n').strip()
def parseentry(self, pos): "Parse a single entry" for entry in BibEntry.instances: if entry.detect(pos): newentry = Cloner.clone(entry) newentry.parse(pos) if not newentry.isvisible(): return if self.showall or newentry.isreferenced(): self.entries.append(newentry) self.added += 1 else: Trace.debug("Ignored entry " + unicode(newentry)) self.ignored += 1 return # Skip the whole line since it's a comment outside an entry pos.globincluding("\n").strip()
def parsetoendings(self, tok, endings): "Parse the tokenizer up to a number of endings." result = '' tok.next() while not tok.current() in endings: processed = self.processtoken(tok) if processed == '++': processed = '+ 1' Trace.debug('Increasing ' + result + ' for endings: ' + unicode(endings)) tok.autoincreases.append(result) if processed == '--': Trace.debug('Decreasing ' + result) processed = '- 1' tok.autodecreases.append(result) if processed != '.' and not result.endswith('.'): processed = ' ' + processed result += processed if not tok.current in endings: tok.next() if len(result) > 0: result = result[1:] Trace.debug('Left after ' + tok.current() + ', endings ' + unicode(endings) + ', result: ' + result) return result
def assigninvoke(self, tok, token=None): "An assignment or a method invocation." self.onelineblock(tok) if not token: token = tok.current() token2 = tok.next() if token2 == '=': # assignment return token + ' = ' + self.parser.parsevalue(tok) if token2 == '.': member = tok.next() return self.assigninvoke(tok, token + '.' + member) if token2 == '(': parameters = self.parser.parseparameters(tok) Trace.debug('Parameters: ' + parameters) return self.assigninvoke(tok, token + parameters) if token2 == '[': square = self.parser.parseinsquare(tok) return self.assigninvoke(tok, token + square) if token2 == '{': # ignore anonymous class self.parser.parseupto('}', tok) return token if token2 == '++': Trace.debug('Increasing invoked ' + token) tok.autoincreases.append(token) return self.assigninvoke(tok, token + ' + 1') if token2 == '--': Trace.debug('Decreasing invoked ' + token) tok.autodecreases.append(token) return self.assigninvoke(tok, token + ' - 1') if token2 in [';', ',', ')']: # finished invocation return token if token2 in tok.javasymbols: Trace.error('Unknown symbol ' + token2 + ' for ' + token) return '*error ' + token + ' ' + token2 + ' error*' token3 = tok.next() if token3 == ';': # a declaration; ignore return '' if token3 == '=': # declaration + assignment tok.variables.append(token2) return token2 + ' = ' + self.parser.parsevalue(tok) if token3 == '[': # array declaration self.parser.parseupto(']', tok) return self.assigninvoke(tok, token2) Trace.error('Unknown combination ' + token + '+' + token2 + '+' + token3) return '*error ' + token + ' ' + token2 + ' ' + token + ' error*'
def assigninvoke(self, tok, token = None): "An assignment or a method invocation." self.onelineblock(tok) if not token: token = tok.current() token2 = next(tok) if token2 == '=': # assignment return token + ' = ' + self.parser.parsevalue(tok) if token2 == '.': member = next(tok) return self.assigninvoke(tok, token + '.' + member) if token2 == '(': parameters = self.parser.parseparameters(tok) Trace.debug('Parameters: ' + parameters) return self.assigninvoke(tok, token + parameters) if token2 == '[': square = self.parser.parseinsquare(tok) return self.assigninvoke(tok, token + square) if token2 == '{': # ignore anonymous class self.parser.parseupto('}', tok) return token if token2 == '++': Trace.debug('Increasing invoked ' + token) tok.autoincreases.append(token) return self.assigninvoke(tok, token + ' + 1') if token2 == '--': Trace.debug('Decreasing invoked ' + token) tok.autodecreases.append(token) return self.assigninvoke(tok, token + ' - 1') if token2 in [';', ',', ')']: # finished invocation return token if token2 in tok.javasymbols: Trace.error('Unknown symbol ' + token2 + ' for ' + token) return '*error ' + token + ' ' + token2 + ' error*' token3 = next(tok) if token3 == ';': # a declaration; ignore return '' if token3 == '=': # declaration + assignment tok.variables.append(token2) return token2 + ' = ' + self.parser.parsevalue(tok) if token3 == '[': # array declaration self.parser.parseupto(']', tok) return self.assigninvoke(tok, token2) Trace.error('Unknown combination ' + token + '+' + token2 + '+' + token3) return '*error ' + token + ' ' + token2 + ' ' + token + ' error*'
def process(self): self.type = 'author' self.output.tag = 'h2 class="author"' author = self.extracttext() Trace.debug('Author: ' + author) DocumentAuthor.appendauthor(author)
def tree(self, level = 0): "Show in a tree" Trace.debug(" " * level + unicode(self)) for container in self.contents: container.tree(level + 1)
def process(self): "Disable inactive branches" self.branch = self.header[2] if not self.isactive(): Trace.debug('Branch ' + self.branch + ' not active') self.output = EmptyOutput()
def tree(self, level=0): "Show in a tree" Trace.debug(" " * level + unicode(self)) for container in self.contents: container.tree(level + 1)