Beispiel #1
0
def main():
    parser = argparse.ArgumentParser(
        description="Process Markdown according to "
        "the CommonMark specification.")
    if sys.version_info < (3, 0):
        reload(sys)  # noqa
        sys.setdefaultencoding('utf-8')
    parser.add_argument(
        'infile',
        nargs="?",
        type=argparse.FileType('r'),
        default=sys.stdin,
        help="Input Markdown file to parse, defaults to STDIN")
    parser.add_argument(
        '-o',
        nargs="?",
        type=argparse.FileType('w'),
        default=sys.stdout,
        help="Output HTML/JSON file, defaults to STDOUT")
    parser.add_argument('-a', action="store_true", help="Print formatted AST")
    parser.add_argument('-aj', action="store_true", help="Output JSON AST")
    args = parser.parse_args()
    parser = CommonMark.Parser()
    f = args.infile
    o = args.o
    lines = []
    for line in f:
        lines.append(line)
    data = "".join(lines)
    ast = parser.parse(data)
    if not args.a and not args.aj:
        renderer = CommonMark.HtmlRenderer()
        o.write(renderer.render(ast))
        exit()
    if args.a:
        # print ast
        CommonMark.dumpAST(ast)
        exit()

    # o.write(ast.to_JSON())
    o.write(CommonMark.dumpJSON(ast))
    exit()
Beispiel #2
0
def main():
    parser = argparse.ArgumentParser(
        description="Process Markdown according to "
        "the CommonMark specification.")
    if sys.version_info < (3, 0):
        reload(sys)  # noqa
        sys.setdefaultencoding('utf-8')
    parser.add_argument('infile',
                        nargs="?",
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="Input Markdown file to parse, defaults to STDIN")
    parser.add_argument('-o',
                        nargs="?",
                        type=argparse.FileType('w'),
                        default=sys.stdout,
                        help="Output HTML/JSON file, defaults to STDOUT")
    parser.add_argument('-a', action="store_true", help="Print formatted AST")
    parser.add_argument('-aj', action="store_true", help="Output JSON AST")
    args = parser.parse_args()
    parser = CommonMark.Parser()
    f = args.infile
    o = args.o
    lines = []
    for line in f:
        lines.append(line)
    data = "".join(lines)
    ast = parser.parse(data)
    if not args.a and not args.aj:
        renderer = CommonMark.HtmlRenderer()
        o.write(renderer.render(ast))
        exit()
    if args.a:
        # print ast
        CommonMark.dumpAST(ast)
        exit()

    # o.write(ast.to_JSON())
    o.write(CommonMark.dumpJSON(ast))
    exit()
Beispiel #3
0
import CommonMark
parser = argparse.ArgumentParser(description="Process Markdown according to the CommonMark specification.")
if sys.version_info < (3, 0):
    reload(sys)
    sys.setdefaultencoding('utf-8')
parser.add_argument('infile', nargs="?", type=argparse.FileType('r'), default=sys.stdin, help="Input Markdown file to parse, defaults to STDIN")
parser.add_argument('-o', nargs="?", type=argparse.FileType('w'), default=sys.stdout, help="Output HTML/JSON file, defaults to STDOUT")
parser.add_argument('-a', action="store_true", help="Print formatted AST")
parser.add_argument('-aj', action="store_true", help="Output JSON AST")
args = parser.parse_args()
parser = CommonMark.DocParser()
f = args.infile
o = args.o
lines = []
for line in f:
    lines.append(line)
data = "".join(lines)
ast = parser.parse(data)
if not args.a and not args.aj:
    renderer = CommonMark.HTMLRenderer()
    o.write(renderer.render(ast))
    exit()
if args.a:
    # print ast
    CommonMark.dumpAST(ast)
    exit()

#o.write(ast.to_JSON())
o.write(CommonMark.ASTtoJSON(ast))
exit()
Beispiel #4
0
 def test_dumpAST_orderedlist(self):
     md = '1.'
     ast = Parser().parse(md)
     CommonMark.dumpAST(ast)
Beispiel #5
0
    help="Input Markdown file to parse, defaults to STDIN")
parser.add_argument(
    '-o',
    nargs="?",
    type=argparse.FileType('w'),
    default=sys.stdout,
    help="Output HTML/JSON file, defaults to STDOUT")
parser.add_argument('-a', action="store_true", help="Print formatted AST")
parser.add_argument('-aj', action="store_true", help="Output JSON AST")
args = parser.parse_args()
parser = CommonMark.Parser()
f = args.infile
o = args.o
lines = []
for line in f:
    lines.append(line)
data = "".join(lines)
ast = parser.parse(data)
if not args.a and not args.aj:
    renderer = CommonMark.HtmlRenderer()
    o.write(renderer.render(ast))
    exit()
if args.a:
    # print ast
    CommonMark.dumpAST(ast)
    exit()

# o.write(ast.to_JSON())
o.write(CommonMark.ASTtoJSON(ast))
exit()
Beispiel #6
0
    def _parse_node(self, node):

        children = node.children + node.inline_content

        if node.t in ("ATXHeader", "SetextHeader"):
            obj = Style(name="Heading {0}".format(node.level))
        elif node.t == "Paragraph":
            obj = Paragraph()
        elif node.t == "Str":
            obj = Text(text=node.c)
        elif node.t in ("Softbreak", "Hardbreak"):
            obj = Text(text=" ")
        elif node.t == "Emph":
            obj = Italic()
        elif node.t == "Strong":
            obj = Bold()
        elif node.t == "Image":
            caption = node.label[0].c if len(node.label) else ""
            obj = Image(location=node.destination, caption=caption)
        elif node.t == "HtmlBlock":
            # Special case. Parse the HTML into instructions
            instructions = self.html_parser.parse(node.string_content)

            if not instructions:
                obj = IgnoredOperation()
            elif len(instructions) == 1:
                # Only contains one instruction, carry on as normal
                return instructions[0]
            else:
                # Lots of instructions. Return a group
                return Group(instructions)
        elif node.t == "List":
            obj = BulletList() if node.list_data["type"] == "Bullet" else NumberedList()
        elif node.t == "ListItem":
            obj = ListElement()
        elif node.t == "Link":
            obj = HyperLink(location=node.destination)
            children.extend(node.label)
        elif node.t in ("ReferenceDef", "HorizontalRule"):
            # ToDo: handle markdown references
            obj = IgnoredOperation()
        elif node.t == "Code":
            # Need an inline code object
            obj = InlineCode([Text(text=node.c)])
        elif node.t == "IndentedCode":
            obj = CodeBlock([Text(text=node.string_content)])
        else:
            CommonMark.dumpAST(node)
            raise ParseException("Cannot process node type {0}".format(node.t))

        if isinstance(node.c, list):
            children = children + node.c

        for child in children:
            result = self._parse_node(child)

            if result is None:
                continue

            obj.add_child(result)

        return obj
 def test_dumpAST_orderedlist(self):
     md = '1.'
     ast = Parser().parse(md)
     CommonMark.dumpAST(ast)
Beispiel #8
0
    while True:
        s = ""
        while True:
            inp = raw_input(colors.OKBLUE + "Markdown: " + colors.ENDC)
            if not inp == "end" and not inp == "quit":
                s += inp + "\n"
            elif inp == "end":
                s = s[:-1]
                break
            elif inp == "quit":
                print(colors.HEADER + "bye!" + colors.ENDC)
                exit(0)
        ast = parser.parse(s)
        html = renderer.render(ast)
        print(colors.WARNING + "=" * 10 + "AST=====" + colors.ENDC)
        CommonMark.dumpAST(ast)
        print(colors.WARNING + "=" * 10 + "HTML====" + colors.ENDC)
        print(html)

# some tests?
if args.t:
    tests = args.t.split(",")
    choice_examples = []
    for t in tests:
        if not t == "" and len(examples) > int(t):
            choice_examples.append(examples[int(t) - 1])
    examples = choice_examples

# all tests

for i, example in enumerate(examples):  # [0,examples[0]]
	while True:
		s = ""
		while True:
			inp = raw_input(colors.OKBLUE+"Markdown: "+colors.ENDC)
			if not inp == "end" and not inp == "quit":
				s += inp+"\n"
			elif inp == "end":
				s = s[:-1]
				break
			elif inp =="quit":
				print(colors.HEADER+"bye!"+colors.ENDC)
				exit(0)
		ast = parser.parse(s)
		html = renderer.render(ast)
		print(colors.WARNING+"="*10+"AST====="+colors.ENDC)
		CommonMark.dumpAST(ast)
		print(colors.WARNING+"="*10+"HTML===="+colors.ENDC)
		print(html)

# some tests?
if args.t:
	tests = args.t.split(",")
	choice_examples = []
	for t in tests:
		if not t == "" and len(examples) > int(t):
			choice_examples.append(examples[int(t)-1])
	examples = choice_examples

# all tests

for i, example in enumerate(examples): # [0,examples[0]]