示例#1
0
 def test_ast_directive(self):
     data = fixtures.load_json('directive.json')
     md = Markdown(renderer=AstRenderer())
     md.use(PluginDirective())
     # Use JSON to fix the differences between tuple and list
     tokens = json.loads(json.dumps(md(data['text'])))
     self.assertEqual(tokens, data['tokens'])
    def __init__(self, renderer=None, inline=None, block=None, **kwargs):
        if not renderer:
            renderer = MarkdownRenderer(**kwargs)
        if not block:
            block = MarkdownBlockProcessor(**kwargs)

        Markdown.__init__(self, renderer, inline, block, **kwargs)
示例#3
0
 def __init__(self):
     renderer = WikiRenderer()
     inline = WikiInlineLexer(renderer)
     # enable the feature
     inline.enable_wiki_page()
     inline.enable_wiki_file()
     self.markdown = Markdown(renderer, inline=inline)
示例#4
0
    def test_published_body(self):
        article = mommy.make('djblog.Article', body='hello')

        self.mock.StubOutWithMock(Markdown, 'render')
        Markdown.render('hello').AndReturn('converted text')

        self.mock.ReplayAll()
        a = article.published_body()
        self.mock.VerifyAll()

        self.assertEqual(a, 'converted text')
示例#5
0
文件: MDtoANKI.py 项目: pyrusx/Ankify
def singlecard(md_file):
    '''converts single md file'''

    renderer = simple()  #Picks which generator to use, cloze or simple
    inline = HighlightsInlineLexer(renderer)
    inline.enable_highlights()
    md = Markdown(renderer=renderer, inline=inline)
    text = open(md_file, encoding='utf-8').read()
    html = md.render(text)
    html = html.replace('\n', '')
    html = html.replace(f'{card_left}', '\n')
    html = html.replace(f'{card_right}', f'{delimiter}')
    html = html.replace('<p>\n', '', 1)
    html = html.replace('images/', '')
    return html
示例#6
0
def md2html(md):
	renderer = LaTexRenderer()
	inline = LaTexInlineLexer(renderer)
	# enable the feature
	inline.enable_LaTex()
	markdown = Markdown(renderer, inline=inline)
	return markdown(md)
示例#7
0
 def save(self, *args, **kwargs):
     # markdown to html
     markdown = Markdown(renderer=BlogContentRenderer())
     self.html_content = markdown(self.md_content)
     # preview content
     bs = BeautifulSoup(self.html_content, 'html.parser')
     p_list = bs.find_all('p')
     preview = ''
     preview_len = 0
     preview_paragraph = 0
     for p_item in p_list:
         p_item_string_len = len(p_item.get_text().strip()) if isinstance(p_item.get_text(), str) else 0
         if p_item_string_len > 0:
             preview += '<p>%s</p>' % p_item.get_text()
             preview_len += p_item_string_len
             preview_paragraph += 1
         if preview_len > 200 or preview_paragraph >= 5:
             break
     self.preview_content = preview
     super(Article, self).save(*args, **kwargs)
     # process images
     # clear old image data
     article_images = ArticleImage.objects.filter(article=self)
     for article_image in article_images:
         article_image.delete()
     # write new data
     img_list = bs.find_all('img')
     for img_item in img_list:
         src = img_item.get('src')
         if not isinstance(src, str) or len(src.strip()) == 0:
             continue
         article_image = ArticleImage(url=src, publish_time=self.publish_time, article=self)
         article_image.save()
示例#8
0
def parse_json_post_data(json_data, post_author):
    data = json.loads(json_data)

    if 'title' not in data:
        raise Exception("No title found in data")

    data["posted"] = datetime.datetime.utcnow()
    markdown = Markdown()
    content = markdown(data['content'])
    content = html.sanitize_html(content)

    data['preview'] = html.html_preview(content)
    data['markdown'] = data['content']
    data['content'] = content
    data['author'] = post_author
    data['img'] = html.get_first_img(content)
    
    if 'topics' in data:
        for count in range(0, len(data['topics'])):
            data['topics'][count] = data['topics'][count].lower()

    # recreate dictionary to remove any unwanted fields the client
    # could have included in the post
    return {
        "title": data["title"],
        "posted": data["posted"],
        "preview": data["preview"],
        "markdown": data["markdown"],
        "content": data["content"],
        "author": data["author"],
        "img": data["author"],
    }
示例#9
0
def run(item: Any, **_kwargs: Any) -> Any:
    if item.path.suffix.lower() == ".md":
        renderer = Render(escape=True)
        markdown = Markdown(renderer=renderer)
        content = markdown(item.path.read_text())
        item = evolve(item, content=content, title=renderer.title)
    return item
示例#10
0
 def render(self, ctx: GenContext) -> RenderedMarkdown:
     """Render Markdown to html, extracting the ToC."""
     link_mapping = self._map_links(ctx)
     renderer = self.renderer(link_mapping)
     html = Markdown(renderer).render(self.text)
     toc = renderer.table_of_contents(level=3)
     preview_html = self._extract_preview(html)
     return RenderedMarkdown(html=html, preview_html=preview_html, toc=toc)
示例#11
0
    def to_pot(self, text):

        renderer = TvhMdRenderer(pot=1)
        renderer.pot_blacklist = read_blacklist()
        md = Markdown(renderer)
        md(text)

        return pot_header('markdown text') + renderer.pot
示例#12
0
 def __init__(self, markdown):
     parser = RecipeParser()
     md = Markdown(parser, extensions=('fenced-code', ))
     md(markdown)
     if parser.current_step is not None:
         raise MarkdownAnnotationError(
             'Reached end of input searching for <!-- {} -->'.format(
                 end_token))
     self.all_steps = parser.all_steps
示例#13
0
    def __init__(self, path: str, dev: bool):
        self.path = path
        self.dev = dev

        kinxfile = load_fm(self.__get_path("Kinxfile"))

        self.markdown = Markdown(
            renderer=Renderer(escape=False, hard_wrap=True),
            inline=InlineLexer,
            block=BlockLexer,
        )

        try:
            try:
                jinja_extensions = kinxfile["extensions"]["jinja"]
            except KeyError:
                jinja_extensions = ()
            self.env = Environment(
                extensions=jinja_extensions,
                autoescape=True,
                loader=FileSystemLoader(self.__get_path(kinxfile["root"])),
            )
            del jinja_extensions
        except KeyError:
            print("Unexpected error: 'root' key not found in Kinxfile")
            raise

        project_dir: dict = {}

        self.headers: List[Union[Tuple[str, int], str]] = [self.DEFAULT_HEADER]
        for i in etree.fromstring(self.markdown(kinxfile.content),
                                  HTMLParser())[0]:
            if i.tag == "ul":
                project_dir[self.headers[-1]].extend(self.__get_links_md(i))
            elif (i.tag[:1] == "h"
                  and i.tag[1:] in (str(j) for j in range(1, 7))):
                self.headers.append((i.text, i.tag[1:]))
                project_dir[self.headers[-1]] = []
            else:
                project_dir[self.headers[-1]].append(i.tag)
                if i.tag not in ["hr"]:
                    print("{} is not read in Kinxfile".format(
                        etree.tostring(i)))

        self.kx: dict = {}
        for i in [
                "title", "author", "description", "url", "copyright", "theme"
        ]:
            self.kx[i] = kinxfile[i]
        del kinxfile

        self.kx["content"] = (project_dir)
        del project_dir

        print(self.kx)

        self.pages: Dict[str, str] = {}
示例#14
0
def default_content_generator(sutra, markdown_parser=None):
    json_doc = sutra.json()
    markdown_parser = markdown_parser or Markdown()
    content = json_doc['content']
    content_parsed = markdown_parser(content).strip()
    content_parsed = content_parsed.replace('\n', '<BR>').replace(
        '<code>', '<strong>').replace('</code>', '</strong>')
    if content_parsed == '':
        print('sutra {} has no content'.format(sutra.index))
    return content_parsed
示例#15
0
def markdown(text):
    from mistune import Markdown, Renderer, escape
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import HtmlFormatter

    class ExtRenderer(Renderer):
        def block_code(self, code, lang):
            if not lang:
                return '\n<pre><code>%s</code></pre>\n' % escape(code)
            lexer = get_lexer_by_name(lang, stripall=True, startinline=True)
            formatter = HtmlFormatter()
            return highlight(code, lexer, formatter)

    md = Markdown(
        renderer=ExtRenderer(use_xhtml=True),
        escape=False, hard_wrap=True
    )
    return md.render(text)
示例#16
0
    def internationalize(self, text, po):

        po = self.po_parse(po)
        renderer = TvhMdRenderer(lang_md=1)
        renderer.strings = self.strings
        renderer.pot_blacklist = read_blacklist()
        md = Markdown(renderer)
        output = md(text)

        return output
def main():
    with open('slides.md') as slides:
        with open('index.template.html') as template:
            with open('index.html', 'w') as output:
                renderer = CodeRunner()
                markdown = Markdown(renderer=renderer)
                slides_html = markdown(slides.read())

                template = template.read()
                print(template.replace('<!-- SLIDES -->', format(slides_html)),
                      file=output)
示例#18
0
    def html(self, text):

        renderer = WEBUI_Renderer(use_xhtml=1)
        md = Markdown(renderer)
        output = md(text)

        hdr = '<!-- Do not edit! Automatically created file: https://github.com/tvheadend/tvheadend-documentation -->\n'
        #now = datetime.datetime.now(UTC())
        #hdr += '<!-- Build date: ' + now.strftime("%F %R %z") + ' -->\n'

        return hdr + '<div class="hts-doc-text">\n' + output + '</div>\n'
示例#19
0
def run(input, name, human):
    fp = utf8open(input, 'r')
    text = fp.read(1024 * 1024 * 2)
    fp.close()

    renderer = TVH_C_Renderer(parse_html=1)
    md = Markdown(renderer)
    text = md(text)
    text = optimize(text)
    if human:
        return text
    return 'const char *' + name + '[] = {\n' + text + '\nNULL\n};\n'
def extract_yaml(markdown_content):
    # Get the YAML code inside the Markdown
    try:
        block = YamlBlockLexer()
        block.enable_autorest_yaml()

        extractor = YamlExtractor()
        markdown_processor = Markdown(extractor, block=block)
        markdown_processor(markdown_content)
        return extract_input_file(extractor.yaml_content)
    except Exception as err:
        raise ValueError("The Markdown/YAML content is not valid: %s",
                         str(err))
示例#21
0
    def parse(self, content, **kwargs):

        newline_type = find_newline_type(content)
        if newline_type == 'DOS':
            content = force_newline_type(content, 'UNIX')

        # mistune expands tabs to 4 spaces and trims trailing spaces, so we
        # need to do the same in order to be able to match the substrings
        template = content.expandtabs(4)
        pattern = re.compile(ensure_unicode(r'^ +$'), re.M)
        content = pattern.sub('', template)
        template = content
        stringset = []

        yml_header = re.match(
            ensure_unicode(r'^(---\s+)([\s\S]*?[^`])\s*(\n---\s+)(?!-)'),
            content)
        yaml_header_content = ''
        yaml_stringset = []
        if yml_header:
            yaml_header_content = yml_header.group()
            md_content = content[len(yaml_header_content):]
            yaml_stringset = self.yaml_parser(yaml_header_content)
        else:
            md_content = content

        block = TxBlockLexer()
        markdown = Markdown(block=block)

        # Making sure stringset is empty because of recursive inside `markdown`
        block.md_stringset = []

        # Command that populates block.stringset var
        markdown(md_content)

        order = 0
        curr_pos = 0
        for string in (yaml_stringset + block.md_stringset):
            string = string_handler(string, template)
            if string and string in template[curr_pos:]:
                string_object = OpenString(six.text_type(order),
                                           string,
                                           order=order)
                order += 1
                stringset.append(string_object)
                # Keep track of the index of the last replaced hash
                template = template[:curr_pos] + template[curr_pos:].replace(
                    string, string_object.template_replacement, 1)
                curr_pos = template.find(string_object.template_replacement)
                curr_pos = curr_pos + len(string_object.template_replacement)
        return force_newline_type(template, newline_type), stringset
示例#22
0
def extract_yaml(markdown_content):
    # Get the YAML code inside the Markdown
    try:
        extractor = YamlExtractor()
        markdown_processor = Markdown(extractor)
        markdown_processor(markdown_content)
        raw_yaml = extractor.yaml_content.getvalue()
    except Exception:
        raise ValueError("The Markdown content is not valid")

    # Get the yaml as a dict
    try:
        return yaml.load(raw_yaml)
    except Exception:
        raise ValueError("Unable to build a valid YAML from this Markdown")
示例#23
0
def create_babylon(
        vritti,
        babylon_file_path,
        babylon_directives_generator=default_babylon_directives_generator,
        headwords_generator=default_headwords_generator,
        content_generator=default_content_generator):

    try:
        os.remove(babylon_file_path)
    except FileNotFoundError:
        pass

    babylon_file = open(babylon_file_path, 'ab')
    babylon_file.write('\n'.encode('utf-8'))

    directives = babylon_directives_generator(vritti)
    for key, val in directives.items():
        directive_line = "#{key}={val}\n".format(key=key, val=val)
        babylon_file.write(directive_line.encode('utf-8'))

    babylon_file.write('\n'.encode('utf-8'))

    markdown_parser = Markdown()

    for adhyaaya in vritti:
        for paada in adhyaaya:
            for sutra in paada:
                head_words = headwords_generator(sutra)
                if not head_words:
                    continue
                content = content_generator(sutra,
                                            markdown_parser=markdown_parser)
                if not content:
                    continue
                headwords_line = '|'.join(head_words) + '\n'
                content_line = content.strip('\n') + '\n'

                babylon_file.write(headwords_line.encode('utf-8'))
                babylon_file.write(content_line.encode('utf-8'))
                babylon_file.write('\n'.encode('utf-8'))

    babylon_file.close()
示例#24
0
文件: markdown.py 项目: GNHua/pwlite
class WikiMarkdown:
    def __init__(self):
        renderer = WikiRenderer()
        inline = WikiInlineLexer(renderer)
        # enable the feature
        inline.enable_wiki_page()
        inline.enable_wiki_file()
        self.markdown = Markdown(renderer, inline=inline)

    def __call__(self, wiki_page, markdown):
        g.wiki_page = wiki_page
        self.markdown.renderer.reset_toc()
        html = self.markdown.parse(markdown)

        # workaround for when there is no headings
        try:
            toc = self.markdown.renderer.render_toc(level=3)
        except TypeError:
            toc = ''
        return toc, html
示例#25
0
def md2html(text,
            styles=None,
            poster="",
            banner="",
            convert_list=True,
            ul_style="\u25CB"):
    md = Markdown()

    # 将markdown列表转化为带序号的普通段落(纯为适应微信中列表序号样式自动丢失的古怪现象)
    if convert_list:
        blocks = text.split("\n```")
        for i in range(0, len(blocks)):
            if i % 2 == 0:
                blocks[i] = re.sub(r"(\n\d+)(\.\s.*?)", r"\n\1\\\2", blocks[i])
                blocks[i] = re.sub(r"\n[\-\+\*](\s.*?)",
                                   u"\n\n{} \1".format(ul_style), blocks[i])
            else:
                continue  # 跳过代码块内部内容
        text = "\n```".join(blocks)

    inner_html = md(text)
    result = premailer.transform(pack_html(inner_html, styles, poster, banner))
    return result
示例#26
0
DISCOURSE_API_KEY = os.getenv("DISCOURSE_API_KEY")
DISCOURSE_API_USERNAME = os.getenv("DISCOURSE_API_USERNAME")

session = requests.get_session()
discourse_api = DiscourseAPI(
    base_url="https://discourse.juju.is/",
    session=session,
    api_key=DISCOURSE_API_KEY,
    api_username=DISCOURSE_API_USERNAME,
)

_yaml = YAML(typ="rt")
_yaml_safe = YAML(typ="safe")

md_parser = Markdown(renderer=Renderer())


def get_yaml_loader(typ="safe"):
    if typ == "safe":
        return _yaml_safe
    return _yaml


def split_filters(filters):
    filter_params = {}

    for charm_filter in filters:
        if charm_filter not in filter_params:
            filter_params[charm_filter] = []
示例#27
0
def markdown_renderer(content):
    renderer = ZhiQueMarkdownRenderer(inlinestyles=False)

    mdp = Markdown(escape=True, renderer=renderer)
    return mark_safe(mdp(content))
示例#28
0
from feedgen.feed import FeedGenerator
from marshmallow import EXCLUDE
from marshmallow.exceptions import ValidationError
from mistune import Markdown
from sortedcontainers import SortedDict
from sqlalchemy import asc, desc, or_
from sqlalchemy.exc import IntegrityError, DataError

# Local
from webapp.security.database import db_session
from webapp.security.models import CVE, Notice, Package, Status, Release
from webapp.security.schemas import CVESchema, NoticeSchema
from webapp.security.auth import authorization_required

markdown_parser = Markdown(hard_wrap=True,
                           parse_block_html=True,
                           parse_inline_html=True)


def notice(notice_id):
    notice = db_session.query(Notice).get(notice_id)

    if not notice:
        flask.abort(404)

    package_descriptions = set()
    release_packages = SortedDict()

    if notice.release_packages:
        for codename, pkgs in notice.release_packages.items():
            release_version = (db_session.query(Release).filter(
示例#29
0
def md2html(text, styles=None, poster=''):
    md = Markdown()
    inner_html = md(text)
    result = premailer.transform(pack_html(inner_html, styles, poster))
    return result
示例#30
0
 def rendered_content(self):
     return mark_safe(Markdown(escape=True).render(self.content))
示例#31
0
 def published_body(self):
     md = Markdown(renderer=SyntaxHighlightRenderer())
     return md.render(self.body)