Ejemplo n.º 1
0
def markdown(value):
    return parse_markdown(
        force_unicode(value),
        extensions=settings.MARKDOWN_EXTENSIONS,
        output_format='html5',
        safe_mode=True,
    )
Ejemplo n.º 2
0
 def test_symbols_in_the_list_item_text_that_should_not_be_interpreted(
         self):
     self.assertEqual(
         parse_markdown(
             '* Item 1 with a # in the text\n* Item 2 with * in the text'),
         '<ul><li>Item 1 with a # in the text</li>'
         '<li>Item 2 with * in the text</li></ul>')
Ejemplo n.º 3
0
def markdown(value):
    return parse_markdown(
        force_unicode(value),
        extensions=settings.MARKDOWN_EXTENSIONS,
        output_format='html5',
        safe_mode=True,
    )
Ejemplo n.º 4
0
def get_markdown_doc(src: Path) -> MarkdownDoc:
    doc = parse_markdown(text=src.read_text(), use_front_matter=True)

    # Delete all nodes up to first header
    first_header = doc.find_first(lambda it: isinstance(it, Header))
    if first_header:
        index = doc.children.index(first_header)
        doc.children = doc.children[index:]
    return doc
Ejemplo n.º 5
0
async def convert_post(post, get_html=False):
    desc_length = 160

    no_markdown = markdown.remove_markdown(post['content'])

    no_markdown = no_markdown.replace('&emsp;', ' ')
    no_markdown = no_markdown.replace('\n', ' ')
    no_markdown = no_markdown.replace('  ', ' ')
    description = no_markdown[:desc_length]

    description = description.strip()
    if '.' in description:
        description, _ = description.rsplit('.', 1)
        description += '.'

    if 'slug' in post:
        slug = post['slug']
    else:
        slug = generate_slug(post['title'])

    if get_html:
        post_html = markdown.parse_markdown(post['content'], nofollow=False)
    else:
        post_html = None

    image = post.get('image')
    if image is not None:
        im_size = get_image_size_cached(image['url'])
        image['size'] = im_size

    read_time = 0
    images = post.get('images', [])
    read_time += len(no_markdown.split()) * 0.25
    for i in range(len(images)):
        read_time += max(12 - i, 3)
    read_time += len(post['content'].split('\n')) * 0.2
    print(read_time)
    read_time_str = timeago(read_time, 'read', False, True)

    return {
        'title': post['title'],
        'text': no_markdown,
        'slug': slug,
        'time': post['datetime'],
        'author': post['author'],
        'timeago': timeago(post['datetime']),
        'content': post['content'],
        'html': post_html,
        'datetime': post['datetime'],
        'image': image,
        'description': description,
        'readtime': read_time_str,
        'images': images,
        'hidden': post.get('hidden', False)
    }
Ejemplo n.º 6
0
def main():
    input_file = sys.argv[1]
    src = Path(input_file).read_text()

    man = parse_markdown(text=src, use_front_matter=False)

    process_code_block(man)
    normalize_h2(man)

    # Transform all h3 options, environment var and exit code to tables

    options_h2 = man.find_first(
        lambda it: isinstance(it, Header) and it.title == "Options"
    )
    environment_h2 = man.find_first(
        lambda it: isinstance(it, Header) and it.title == "Environment"
    )
    exit_codes_h2 = man.find_first(
        lambda it: isinstance(it, Header) and it.title == "Exit Codes"
    )
    www_h2 = man.find_first(lambda it: isinstance(it, Header) and it.title == "WWW")

    first_option_h3 = man.find_first(
        lambda it: isinstance(it, Header) and it.level == 3, start=options_h2
    )
    options = man.slice(first_option_h3, environment_h2)
    process_table(doc=man, nodes=options, col_name="Option")

    first_env_h3 = man.find_first(
        lambda it: isinstance(it, Header) and it.level == 3, start=environment_h2
    )
    envs = man.slice(first_env_h3, exit_codes_h2)
    process_table(doc=man, nodes=envs, col_name="Variable")

    first_exit_h3 = man.find_first(
        lambda it: isinstance(it, Header) and it.level == 3, start=exit_codes_h2
    )
    exits = man.slice(first_exit_h3, www_h2)
    process_table(doc=man, nodes=exits, col_name="Value")

    print(header() + man.to_text())
Ejemplo n.º 7
0
 def test_little_bit_of_everything(self):
     self.assertEqual(
         parse_markdown('# Header!\n* __Bold Item__\n* _Italic Item_'),
         '<h1>Header!</h1><ul><li><strong>Bold Item</strong></li>'
         '<li><em>Italic Item</em></li></ul>')
Ejemplo n.º 8
0
 def test_h6(self):
     self.assertEqual(parse_markdown('###### This will be an h6'),
                      '<h6>This will be an h6</h6>')
Ejemplo n.º 9
0
 def test_h1(self):
     self.assertEqual(parse_markdown('# This will be an h1'),
                      '<h1>This will be an h1</h1>')
Ejemplo n.º 10
0
 def test_bold(self):
     self.assertEqual(parse_markdown('__This will be bold__'),
                      '<p><strong>This will be bold</strong></p>')
Ejemplo n.º 11
0
def markdown(text):
    return parse_markdown(text)
Ejemplo n.º 12
0
 def test_symbols_in_the_paragraph_text_that_should_not_be_interpreted(
         self):
     self.assertEqual(
         parse_markdown('This is a paragraph with # and * in the text'),
         '<p>This is a paragraph with # and * in the text</p>')
Ejemplo n.º 13
0
 def test_symbols_in_the_header_text_that_should_not_be_interpreted(self):
     self.assertEqual(
         parse_markdown('# This is a header with # and * in the text'),
         '<h1>This is a header with # and * in the text</h1>')
Ejemplo n.º 14
0
 def test_bold(self):
     self.assertEqual(
         parse_markdown("__This will be bold__"),
         "<p><strong>This will be bold</strong></p>",
     )
Ejemplo n.º 15
0
def markdown(text):
    return parse_markdown(text)
Ejemplo n.º 16
0
 def test_paragraph(self):
     self.assertEqual(
         parse_markdown("This will be a paragraph"),
         "<p>This will be a paragraph</p>",
     )
Ejemplo n.º 17
0
 def test_unordered_lists(self):
     self.assertEqual(
         parse_markdown("* Item 1\n* Item 2"),
         "<ul><li>Item 1</li>"
         "<li>Item 2</li></ul>",
     )
Ejemplo n.º 18
0
 def test_h6(self):
     self.assertEqual(parse_markdown("###### This will be an h6"),
                      "<h6>This will be an h6</h6>")
Ejemplo n.º 19
0
 def test_h2(self):
     self.assertEqual(parse_markdown("## This will be an h2"),
                      "<h2>This will be an h2</h2>")
Ejemplo n.º 20
0
 def test_h1(self):
     self.assertEqual(parse_markdown("# This will be an h1"),
                      "<h1>This will be an h1</h1>")
Ejemplo n.º 21
0
 def test_unordered_lists_close_properly_with_preceding_and_following_lines(
         self):
     self.assertEqual(
         parse_markdown('# Start a list\n* Item 1\n* Item 2\nEnd a list'),
         '<h1>Start a list</h1><ul><li>Item 1</li>'
         '<li>Item 2</li></ul><p>End a list</p>')
Ejemplo n.º 22
0
 def test_italics(self):
     self.assertEqual(
         parse_markdown("_This will be italic_"),
         "<p><em>This will be italic</em></p>",
     )
Ejemplo n.º 23
0
 def test_italics(self):
     self.assertEqual(parse_markdown('_This will be italic_'),
                      '<p><em>This will be italic</em></p>')
Ejemplo n.º 24
0
 def test_paragraph(self):
     self.assertEqual(parse_markdown('This will be a paragraph'),
                      '<p>This will be a paragraph</p>')
Ejemplo n.º 25
0
 def test_bold(self):
     self.assertEqual(parse_markdown('__This will be bold__'),
                      '<p><strong>This will be bold</strong></p>')
Ejemplo n.º 26
0
 def test_italics(self):
     self.assertEqual(parse_markdown('_This will be italic_'),
                      '<p><em>This will be italic</em></p>')
Ejemplo n.º 27
0
 def test_mixed_normal_italics_and_bold(self):
     self.assertEqual(parse_markdown('This will _be_ __mixed__'),
                      '<p>This will <em>be</em> <strong>mixed</strong></p>')
Ejemplo n.º 28
0
 def test_mixed(self):
     self.assertEqual(
         parse_markdown('This will _be_ __mixed__'),
         '<p>This will <em>be</em> <strong>mixed</strong></p>')
Ejemplo n.º 29
0
 def test_h1(self):
     self.assertEqual(parse_markdown('# This will be an h1'),
                      '<h1>This will be an h1</h1>')
Ejemplo n.º 30
0
 def test_h2(self):
     self.assertEqual(parse_markdown('## This will be an h2'),
                      '<h2>This will be an h2</h2>')
Ejemplo n.º 31
0
 def test_h2(self):
     self.assertEqual(parse_markdown('## This will be an h2'),
                      '<h2>This will be an h2</h2>')
Ejemplo n.º 32
0
 def test_unordered_lists(self):
     self.assertEqual(
         parse_markdown('* Item 1\n* Item 2'), '<ul><li><p>Item 1</p></li>'
         '<li><p>Item 2</p></li></ul>')
Ejemplo n.º 33
0
def main(dest: str) -> int:

    header: str

    if dest == "github":
        header = dedent("""\
            <img src="https://raw.githubusercontent.com/Orange-OpenSource/hurl/master/docs/logo-dark.svg?sanitize=true#gh-dark-mode-only" alt="Hurl Logo" width="264px"><img src="https://raw.githubusercontent.com/Orange-OpenSource/hurl/master/docs/logo-light.svg?sanitize=true#gh-light-mode-only" alt="Hurl Logo" width="264px">
            
            <br/>
            
            [![deploy status](https://github.com/Orange-OpenSource/hurl/workflows/CI/badge.svg)](https://github.com/Orange-OpenSource/hurl/actions)
            [![CircleCI](https://circleci.com/gh/lepapareil/hurl/tree/master.svg?style=shield)](https://circleci.com/gh/lepapareil/hurl/tree/master)
            [![Crates.io](https://img.shields.io/crates/v/hurl.svg)](https://crates.io/crates/hurl)
            [![documentation](https://img.shields.io/badge/-documentation-informational)](https://hurl.dev)
            
            """)
    elif dest == "crates":
        header = dedent("""\
            <img src="https://raw.githubusercontent.com/Orange-OpenSource/hurl/master/docs/logo-light.svg" alt="Hurl Logo" width="264px">
            
            <br/>
            
            [![deploy status](https://github.com/Orange-OpenSource/hurl/workflows/CI/badge.svg)](https://github.com/Orange-OpenSource/hurl/actions)
            [![CircleCI](https://circleci.com/gh/lepapareil/hurl/tree/master.svg?style=shield)](https://circleci.com/gh/lepapareil/hurl/tree/master)
            [![Crates.io](https://img.shields.io/crates/v/hurl.svg)](https://crates.io/crates/hurl)
            [![documentation](https://img.shields.io/badge/-documentation-informational)](https://hurl.dev)
            
            """)
    else:
        sys.stderr.write("build_readme.py [github, crates]\n")
        return os.EX_USAGE

    header_md = parse_markdown(text=header, use_front_matter=False)

    home_md = get_markdown_doc(src=Path("hurl.dev/index.md"))
    samples_md = get_markdown_doc(src=Path("hurl.dev/_docs/samples.md"))
    usage_md = get_markdown_doc(src=Path("hurl.dev/_docs/man-page.md"))
    installation_md = get_markdown_doc(
        src=Path("hurl.dev/_docs/installation.md"))

    # Process Home / Why Hurl
    # We adapt the "Why Hurl" part to transform h2 tag back to markdown
    def showcase_rep(m):
        return f"<li><b>{m.group(1)}:</b> {m.group(2).lower()}</li>"

    why_paragraph = home_md.find_first(
        lambda it: '<ul class="showcase-container">' in it.content)
    r = re.compile(
        r'<li class="showcase-item"><h2 class="showcase-item-title">(.+?)<\/h2>(.+?)<\/li>',
        re.DOTALL,
    )
    why_paragraph.content = r.sub(showcase_rep, why_paragraph.content)

    body_md = MarkdownDoc()
    body_md.extend(samples_md)
    body_md.extend(usage_md)
    body_md.extend(installation_md)
    toc = body_md.toc()
    toc_md = parse_markdown(text=toc, use_front_matter=False)

    readme_md = MarkdownDoc()
    readme_md.extend(header_md)

    readme_md.extend(home_md)
    readme_md.extend(toc_md)
    readme_md.extend(body_md)

    readme = readme_md.to_text()

    print(readme)
    return os.EX_OK
Ejemplo n.º 34
0
 def test_paragraph(self):
     self.assertEqual(parse_markdown('This will be a paragraph'),
                      '<p>This will be a paragraph</p>')
Ejemplo n.º 35
0
 def test_little_bit_of_everything(self):
     self.assertEqual(parse_markdown(
         '# Header!\n* __Bold Item__\n* _Italic Item_'),
         '<h1>Header!</h1><ul><li><strong>Bold Item</strong></li>'
         '<li><em>Italic Item</em></li></ul>')
Ejemplo n.º 36
0
 def test_h6(self):
     self.assertEqual(parse_markdown(
         '###### This will be an h6'), '<h6>This will be an h6</h6>')
Ejemplo n.º 37
0
 def test_mixed_normal_italics_and_bold(self):
     self.assertEqual(
         parse_markdown("This will _be_ __mixed__"),
         "<p>This will <em>be</em> <strong>mixed</strong></p>",
     )
Ejemplo n.º 38
0
 def test_unordered_lists(self):
     self.assertEqual(parse_markdown('* Item 1\n* Item 2'),
                      '<ul><li>Item 1</li>'
                      '<li>Item 2</li></ul>')
Ejemplo n.º 39
0
def markdown(item, ctx):
    item['payload'] = parse_markdown(item['payload'])
    return item