ap.add_argument( '-e', '--encoding', default='utf-8', help='specify the input character encoding (defaults to utf-8)') ap.add_argument('-s', '--shorthand', nargs='*', default=[], help='add a Shorthand argument (e.g. -s "#id" ".class")') args = ap.parse_args() # Get the standard input binary buffer and wrap it in a file-object so that it # decodes into a stream of Unicode characters from the specified encoding. We # do this without Python translating any linebreaks (omit the newline argument # if you like the default behaviour; the parser can cope with either). fp = io.TextIOWrapper(sys.stdin.buffer, encoding=args.encoding, newline='') shorthandMapping = {} # Convert the commandline arguments into a dict {shorthand: longhand} for i in args.shorthand: assert len(i) >= 2 left = i[0] right = i[1:] shorthandMapping[left] = right tree = bach.parse(fp, shorthandMapping) print(repr(tree))
if label == 'b': yield '\\textbf{%s}' % text.strip() elif label == 'p': yield '%s\n' % NiceText(text.strip()) else: yield LatexEscape(text) else: raise Exception("Don't know what to do with: %s" % label) ap = argparse.ArgumentParser( description='Takes an "article style" bach document from stdin and writes a LaTeX document to stdout') ap.add_argument('-e', '--encoding', default='utf-8', help='specify the input character encoding (defaults to utf-8)') args = ap.parse_args() # Get the standard input binary buffer and wrap it in a file-object so that it # decodes into a stream of Unicode characters from the specified encoding. fp = io.TextIOWrapper(sys.stdin.buffer, encoding=args.encoding) tree = bach.parse(fp) StartDocument(tree)
# https://github.com/tawesoft/bach/issues/2 import bach import io import sys # OK: # document = bach.parse(io.StringIO('point xx="1" yy="2" zz="3"\n')) # Was failing: document = bach.parse(io.StringIO('point x="1" y="2" z="3"\n')) label, attributes, subdocuments = document assert label == "point" assert len(attributes) == 3 assert attributes['x'] == ["1"] assert attributes['y'] == ["2"] assert attributes['z'] == ["3"] assert len(subdocuments) == 0
if label == 'b': yield '\\textbf{%s}' % text.strip() elif label == 'p': yield '%s\n' % NiceText(text.strip()) else: yield LatexEscape(text) else: raise Exception("Don't know what to do with: %s" % label) ap = argparse.ArgumentParser( description= 'Takes an "article style" bach document from stdin and writes a LaTeX document to stdout' ) ap.add_argument( '-e', '--encoding', default='utf-8', help='specify the input character encoding (defaults to utf-8)') args = ap.parse_args() # Get the standard input binary buffer and wrap it in a file-object so that it # decodes into a stream of Unicode characters from the specified encoding. fp = io.TextIOWrapper(sys.stdin.buffer, encoding=args.encoding) tree = bach.parse(fp) StartDocument(tree)
ap = argparse.ArgumentParser() ap.add_argument('-e', '--encoding', default='utf-8', help='specify the input character encoding (defaults to utf-8)') args = ap.parse_args() fp = io.TextIOWrapper(sys.stdin.buffer, encoding=args.encoding, newline='') # mapping from shorthand attribute to longhand attribute shorthandMapping = \ { '#': 'id', '.': 'class', } tree = bach.parse(fp, shorthandMapping) # convert to an ElementTree rootElement = bach.toElementTree(ET, tree) print("Converted into an ElementTree") print(ET.tostring(rootElement)) print("Now running XPATH query: //span") find = ET.XPath("//span") for i in find(rootElement): print(" Text of result is: %s" % repr(i.text)) print("Now running cssselect for span.fancy") find = GenericTranslator().css_to_xpath('span.fancy') print("This translates into %s" % repr(find))
""" Parses a Bach document from stdin - does nothing if there's no error """ import io import sys import bach # Get the standard input binary buffer and wrap it in a file-object so that it # decodes into a stream of Unicode characters from the specified encoding. We # do this without Python translating any linebreaks (omit the newline argument # if you like the default behaviour; the parser can cope with either). fp = io.TextIOWrapper(sys.stdin.buffer, encoding=sys.stdin.encoding) tree = bach.parse(fp, {})