예제 #1
0
 def test_nested_quotes(self):
     expected_output = self.load_md("nested_quote2")
     quotebox = md.MarkdownQuotebox()
     quotebox.add(md.MarkdownParagraph("Paragraph 1"))
     nested_quotebox = md.MarkdownQuotebox()
     nested_quotebox.add(md.MarkdownParagraph("Paragraph 2 (nested)"))
     quotebox.add(nested_quotebox)
     quotebox.add(md.MarkdownParagraph("Paragraph 3"))
     self.assertEqual(expected_output, str(quotebox))
     expected_output = self.load_md("nested_quote3")
     level_0 = md.MarkdownDocument()
     level_1 = md.MarkdownQuotebox()
     level_2 = md.MarkdownQuotebox()
     level_3 = md.MarkdownQuotebox()
     level_0.add(md.MarkdownParagraph("Outside quotes!"))
     level_1.add(md.MarkdownParagraph("First level"))
     level_2.add(md.MarkdownParagraph("Second level"))
     level_3.add(md.MarkdownParagraph("Third level"))
     level_0.add(level_1)
     level_1.add(level_2)
     level_2.add(level_3)
     level_1.add(md.MarkdownParagraph("Back on the first level"))
     level_0.add(
         md.MarkdownLink("Link at the end of the document",
                         "https://nowhere.com"))
     self.assertEqual(expected_output, str(level_0))
예제 #2
0
def process_img(img):
    # use alt text instead if available
    if img.has_attr("alt") and img["alt"]:
        text = img["alt"]
    else:
        text = "Image Link"
    link = img["src"]
    return md.MarkdownLink(text, link)
예제 #3
0
def process_iframe(iframe):
    # these are used for youtube videos.  I'll make the text something else
    # if the src doesn't point to youtube
    src = iframe["src"]
    if "youtube" in src:
        text = "Youtube Video"
    else:
        text = "Embedded content"
    # do some filtering for different kinds of youtube links
    id_indicators = ["youtube.com/embed/", "youtube.com/watch?v="]
    video_id = ""
    for indic in id_indicators:
        if src.find(indic) != -1:
            video_id = src[src.find(indic) + len(indic):]

    if video_id:
        link = "https://www.youtube.com/watch?v=" + video_id
    else:
        link = src
    return str(md.MarkdownLink(text, link)) + ITEM_SEPARATOR
예제 #4
0
 def test_link_with_parentheses(self):
     expected_output = self.load_md("link_escapes")
     link = md.MarkdownLink(
         "escaped parentheses",
         "https://example.com/long_item_name_with(parentheses)")
     self.assertEqual(expected_output, str(link))
예제 #5
0
 def test_link(self):
     expected_output = self.load_md("link")
     link = md.MarkdownLink("link text", "example.com/link")
     self.assertEqual(expected_output, str(link))
예제 #6
0
def convert_tag(tag):
    content_inside_this_tag = process_tag(tag)
    # tags that don't auto insert newline are processed first
    if tag.name == "a" and tag.has_attr("href"):
        return md.MarkdownLink(content_inside_this_tag, tag["href"])
    elif tag.name == "a":
        return content_inside_this_tag
    # these can be handled the same as far as I can tell
    elif tag.name == "code" or tag.name == "pre":
        return process_code(tag)
    elif tag.name == "strong":
        return process_strong(content_inside_this_tag)
    elif tag.name == "span":
        return content_inside_this_tag
    elif tag.name == "em":
        return process_italics(content_inside_this_tag)
    elif tag.name == "img":
        return process_img(tag)
    elif tag.name == "table":
        return process_table(tag)
    elif tag.name == "ul":
        temp = ITEM_SEPARATOR + content_inside_this_tag
        if len(temp) > 2 and temp[-2:] != "\n\n":
            temp += "\n"
        return temp
    elif tag.name == "br":
        return "\n\n"
    elif is_excluded_tag(tag):
        return ""

    # tags below here insert a newline if not already inserted by previous call to convert_tag
    if tag.name == "p":
        output = md.MarkdownParagraph(content_inside_this_tag)
    elif tag.name == "div":
        output = md.MarkdownParagraph(content_inside_this_tag)
    elif tag.name == "h2":
        output = md.MarkdownHeader(content_inside_this_tag, 2)
    elif tag.name == "h3":
        output = md.MarkdownHeader(content_inside_this_tag, 3)
    elif tag.name == "li":
        output = md.MarkdownListItem(content_inside_this_tag)
    elif tag.name == "blockquote":
        output = process_quote_box(tag, content_inside_this_tag)
    elif tag.name == "iframe":
        output = process_iframe(tag)
    elif tag.name == "video":
        if tag.has_attr("src"):
            output = str(md.MarkdownLink("Embedded Video",
                                         tag["src"])) + ITEM_SEPARATOR
        elif tag.children is not None and tag.find("source").has_attr("src"):
            output = str(
                md.MarkdownLink("Embedded Video",
                                tag.find("source")["src"])) + ITEM_SEPARATOR
        else:
            output = md.MarkdownParagraph(content_inside_this_tag)
    else:
        # default case should be fine for remaining tags
        output = content_inside_this_tag
    #if isinstance(output, md.MarkdownQuotebox) and len(output.items) == 3 and "Level 1 Cast when" in output.items[0]:
    #    print(output.items)
    return output