Exemplo n.º 1
0
    def text(self, lang=None, link=None):

        # TODO(takashi) switch with language

        fp = codecs.open(self.filepath, 'r', encoding="utf-8")
        text = fp.read()
        fp.close()

        if link is None:
            return markdown2.Markdown().convert(text)
        else:
            return self._build_link(markdown2.Markdown().convert(text))
Exemplo n.º 2
0
    def get(self, request):
        about = About.objects.all()[0]

        md = markdown2.Markdown(extras=["toc", "header-ids"])
        content = md.convert(about.content)

        return render(request, 'about.html', {'content': content})
Exemplo n.º 3
0
def show(request, alias):
    return render(
        request, 'paste_as_markdown/show.html', {
            'content':
            markdown2.Markdown().convert(str(
                Markdown.objects.get(alias=alias)))
        })
Exemplo n.º 4
0
def new_page(request):
    if request.method == "POST":
        form = NewPage(request.POST)
        if form.is_valid():
            title = form.cleaned_data["title"]
            entries = util.list_entries()
            if title in entries:
                return render(
                    request, "encyclopedia/new.html", {
                        "Form": NewPage(),
                        "Message": "Entry with this name already exists"
                    })
            data = form.cleaned_data["data"]
            util.save_entry(title, data)
            entry = util.get_entry(title)
            md = markdown2.Markdown()
            entry = md.convert(entry)
            return render(request, "encyclopedia/page.html", {
                "entry": entry,
                "heading": title
            })
        else:
            return HttpResponse("There was a Problem!")
    else:
        return render(request, "encyclopedia/new.html", {"Form": NewPage()})
Exemplo n.º 5
0
def get_markdown():
    """
    Find and set a valid markdown parser
    Checks for one of these parsers:
        - pandoc
        - markdown2
    """
    markdown = None
    if shutil.which('pandoc'):
        markdown = lambda x: subprocess.run(
            ["pandoc", "-f", "markdown", "-t", "html"],
            input=x.encode("utf-8"),
            stdout=subprocess.PIPE).stdout.decode("utf-8")
    else:
        try:
            import markdown2
            _md_extras = [
                "code-friendly",
                "fenced-code-blocks",
                "footnotes",
                "header-ids",
            ]
            markdown = markdown2.Markdown(extras=_md_extras).convert
        except ImportError:
            pass

    if markdown is None:
        print("No markdown parser found. Will serve as plain text.")
    return markdown
Exemplo n.º 6
0
def markdown(string):
    # markdown2 writer
    markdowner = markdown2.Markdown()
    result = markdowner.convert(string)
    # put the class title inside the h1 tag
    result = result.replace("<h1>", "<h1 class=\"title\">")
    return result
Exemplo n.º 7
0
def get_markdown():
    markdown = None

    if shutil.which("pd"):
        markdown = lambda x: subprocess.run(
            ["pd", "-f", "markdown", "-t", "html"],
            input = x.encode("utf-8"),
            stdout = subprocess.PIPE).stdout.decode("utf-8")

        else:
            try:
                import markdown2
                _md_extras = [
                    "code-friendly",
                    "fenced-code-blocks",
                    "footnotes",
                    "header-ids"]
                markdown = markdown2.Markdown(extras = _md_extras).convert

            except ImportError:
                pass

            if markdown is None:
                css = ""
                if os.path.isfile("css.css"): #need to rename
                    css = open("css.css").read() #need to rename
                elif os.path.isfile(os.path.expanduser("~/.css.css")):
                    css = open(os.path.expanduser("~/.css.css"))
                return css
Exemplo n.º 8
0
def render_template(template_name, **kwargs):
    data_path = os.path.dirname(os.path.abspath(__file__))
    environment = jinja2.Environment(
        loader=jinja2.FileSystemLoader("{}/templates".format(data_path)),
        autoescape=jinja2.select_autoescape(default=True))

    md = markdown2.Markdown(extras=[
        'fenced-code-blocks',
        'cuddled-lists',
        'tables'])
    def markdown_filter(value):
        return jinja2.Markup(md.convert(value))
    environment.filters['markdown'] = markdown_filter

    def unundef(value):
        return "" if value == ":undef" else value
    environment.filters['unundef'] = unundef

    _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')
    def nl2br(value):
        return jinja2.Markup(
            u'\n\n'.join(u'<p>%s</p>'
                % jinja2.Markup.escape(p) for p in _paragraph_re.split(value)))
    environment.filters['nl2br'] = nl2br

    body_id = re.sub(r"\W+", "_", re.sub(r"\..*", "", template_name))
    template = environment.get_template(template_name)
    return template.render(body_id=body_id, **kwargs)
Exemplo n.º 9
0
  def loadHTML(self):
    path = HelpHTMLDialog.HELP_PAGE_PATH
    content = ''
    with open(path, encoding='utf-8') as file:
      # Load file line by line
      while True:
        line = file.readline()
        content += line
        if not line:
          break
    
    # Convert the page from Markkdown to HTML
    md = markdown2.Markdown()
    html = md.convert(content)
    
    # Wrap the page content with the basic HTML structure and add the scrip which focuses the first element
    html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
''' + html + '''
</body>
</html>
'''
    return html
Exemplo n.º 10
0
def index():
    """Dummy index parsing the readme every time"""
    mk = markdown2.Markdown()
    with open('README.md') as f:
        html = mk.convert(f.read())

    return html, 200
Exemplo n.º 11
0
def generate_from_schema(
    schema: Dict[str, Any],
    minify: bool = False,
    deprecated_from_description: bool = False,
    default_from_description: bool = False,
) -> str:
    md = markdown2.Markdown(extras=["fenced-code-blocks"])
    env = jinja2.Environment()
    env.filters["markdown"] = lambda text: jinja2.Markup(md.convert(text))
    env.filters["python_to_json"] = python_to_json
    env.filters[
        "get_default"] = get_default_look_in_description if default_from_description else get_default
    env.filters["get_type_name"] = get_type_name
    env.filters[
        "get_description"] = get_description_remove_default if default_from_description else get_description
    env.filters["resolve_ref"] = resolve_ref
    env.filters[
        "get_numeric_restrictions_text"] = get_numeric_restrictions_text
    env.tests["combining"] = is_combining
    env.tests["description_short"] = is_description_short
    env.tests[
        "deprecated"] = is_deprecated_look_in_description if deprecated_from_description else is_deprecated

    template_path = os.path.join(os.path.dirname(__file__), TEMPLATE_FILE_NAME)
    with open(template_path, "r") as template_fp:
        template = env.from_string(template_fp.read())

    rendered = template.render(schema=schema)
    if minify:
        rendered = htmlmin.minify(rendered)

    return rendered
Exemplo n.º 12
0
    def __init__(self, site_url, input_dir, output_dir, home=None):
        self.site_url = site_url
        self.input_dir = input_dir
        self.output_dir = output_dir
        self.home = home

        self.verbose = False
        self.quiet = False

        self.config_dir = _join(self.input_dir, "_transom")
        self.config_file = _join(self.config_dir, "config.py")
        self.config = None

        self.outer_template_path = _join(self.config_dir,
                                         "outer-template.html")
        self.inner_template_path = _join(self.config_dir,
                                         "inner-template.html")

        self.input_files = list()
        self.output_files = list()
        self.config_files = list()

        self.links = _defaultdict(set)
        self.link_targets = set()

        self.ignored_file_patterns = [
            "*/.git",
            "*/.svn",
        ]

        self.ignored_link_patterns = list()

        self._markdown_converter = _markdown.Markdown(extras=_markdown_extras)

        self.start_time = None
Exemplo n.º 13
0
def editpage(request, name):
    if request.method == "POST":
        form = EditPage(request.POST)
        if form.is_valid():
            title = name
            content = form.cleaned_data["content"]
            # save it as a new file in markdown
            util.save_entry(title, content)
            markdowned = markdown2.Markdown()
            entry = util.get_entry(title)
            newPage = markdowned.convert(entry)
            return render(request, "encyclopedia/page.html", {
                "page": newPage,
                "title": title,
                "search_form": Search()
            })
        else:
            return render(request, "{% url 'create' %}", {"form": form})
    else:
        page = util.get_entry(name)
        title = name
        form = EditPage(initial={'content': page})
        print(form)
        return render(request, "encyclopedia/edit.html", {
            "page": page,
            "title": title,
            "form": form
        })
Exemplo n.º 14
0
def md_to_html(file_path):
    """
    Return html files from markdown.

    Currently the makrdown2 module is downgraded for the XSS
    vulnerability.

    Parameters
    ----------
    file_path: string
        path to markdown file

    Returns
    -------
    boolean
        Whether or not this completed sucessfully

    """
    try:
        if os.path.exists(file_path) and '.md' in file_path:
            markdowner = markdown2.Markdown()
            md_file = open(file_path, 'r', errors='ignore').read()
            out_text = markdowner.convert(md_file)
            out_file = open(file_path.replace('.md', '.html'), 'w')
            out_file.write(out_text)
            out_file.close()

            return True
        return False
    except PermissionError as exception:
        sys.stderr.write(str(exception))
        return False
    return False
Exemplo n.º 15
0
def index(request):
    if request.method == "POST":
        search_form = Search(request.POST)
        if search_form.is_valid():
            search = search_form.cleaned_data["searched"]
            if search in util.list_entries():
                entry = util.get_entry(search)
                markeddown = markdown2.Markdown()
                page = markeddown.convert(entry)
                return render(request, "encyclopedia/page.html", {
                    "page": page,
                    "title": search,
                    "search_form": Search()
                })
            else:
                compared_pages = []
                for page in util.list_entries():
                    if search.lower() in page.lower():
                        compared_pages.append(page)

                return render(request, "encyclopedia/search.html", {
                    "pages": compared_pages,
                    "search_form": Search()
                })
        else:
            return render(request, "encyclopedia/index.html", {
                "search_form": Search(),
                "entries": util.list_entries()
            })
    else:
        return render(request, "encyclopedia/index.html", {
            "entries": util.list_entries(),
            "search_form": Search()
        })
Exemplo n.º 16
0
def get_links_from_md(file_path, markdowner=markdown2.Markdown()):
    with open(file_path) as f:
        md = f.read()
    html = markdowner.convert(md)
    soup = BeautifulSoup(html, 'html.parser')
    l = [[file_path, a.text, a.attrs.get('href')] for a in soup.find_all('a')]
    return l
Exemplo n.º 17
0
def search(request):
    mark = md.Markdown()
    if request.method == "POST":
        query = request.POST['q']
        entries = util.list_entries()
        matches = []
        for entry in entries:
            if query.lower() == entry.lower():
                return render(request, "encyclopedia/title.html", {
                    "title": entry,
                    "content": mark.convert(util.get_entry(entry))
                })
        else:
            for entry in entries:
                if query.lower() in entry.lower():
                    matches.append(entry)
            if len(matches) == 0:
                return render(request, "encyclopedia/error.html", {
                    "title": query,
                    "already": False
                })
            return render(request, "encyclopedia/search.html", {
                "query": query,
                "matches": matches
            })
Exemplo n.º 18
0
class ArticlePageHandler(RequestHandler):
    md = markdown2.Markdown()

    def get(self, article_link):
        # Select article
        if article_link in articles_data.keys():
            article_data = articles_data[article_link]
            article_path = "..\\articles\\{0}\\article.md".format(
                article_data["file-folder"])

            # Open markdown file, read it, convert it to html, close the file
            f = open(os.path.join(os.path.dirname(__file__), article_path),
                     "rb")
            body_html = self.md.convert(f.read())
            f.close()

            # Render the template. Please mind the 'raw' function in the template for body_html
            self.render(get_template_path("article.html"),
                        page_title="MotoBLOG - " + article_data["title"],
                        nav=default_nav,
                        article_data=article_data,
                        body_html=body_html)
        else:
            # Article link not found in list defined by articles.json... Show error page
            error_body = "The article with link '' was not found. Please check the url. If a link is broken please report it.".format(
                article_link)
            self.render(get_template_path("error.html"),
                        page_title="Error page",
                        error_title="Article not found",
                        error_body=error_body)
Exemplo n.º 19
0
def randomPage(request):
    mark = md.Markdown()
    entries = util.list_entries()
    entry = random.choice(entries)
    return render(request, "encyclopedia/title.html", {
        "title": entry,
        "content": mark.convert(util.get_entry(entry))
    })
Exemplo n.º 20
0
 def markdown_to_html(description):
     markdowner = markdown2.Markdown(extras={'demote-headers': 3})
     # convert markdown to html
     html_description = markdowner.convert(description)
     # Remove code blocks
     html_description = html_description.replace('<code>', '').replace(
            '</code>','').replace('<pre>','').replace('</pre>','')
     return html_description
Exemplo n.º 21
0
class MarkdownField(CharField):
    parser = markdown2.Markdown(safe_mode=False)

    def to_representation(self, value):
        return self.render(super().to_representation(value))

    def render(self, text):
        return self.parser.convert(text)
Exemplo n.º 22
0
 def save(self, *args, **kwargs):
     if not self.excerpt:
         md = markdown2.Markdown(extras=[
             'code-friendly',
             'fenced-code-blocks',
         ])
         self.excerpt = strip_tags(md.convert(self.body)[:54])
     super(Post, self).save(*args, **kwargs)
Exemplo n.º 23
0
def markdown_to_html(markdownText, images):
    # md = markdown2.Markdown()
    image_ref = ""
    for image in images:
        image_url = settings.MEDIA_URL + image.image.url
        image_ref = "%s\n[%s]: %s" % (image_ref, image, image_url)

    md = "%s\n%s" % (markdownText, image_ref)
    html = markdown2.Markdown(md)
Exemplo n.º 24
0
def getmark(request):
    mark = markdown2.Markdown()
    mark.convert(entry)
    """
    Convert given markdown to html.
    :param md: string
    :return: string - converted html
    """
    return render(request, "encyclopedia/random.html", {util.get_entry()})
Exemplo n.º 25
0
def search(request):
    mark = markdown2.Markdown()
    if request.method == "POST":
        title = request.POST.get("q")
    else:
        return HttpResponse("Resubmit form!")
    return render(request, "encyclopedia/get.html", {
        "data": mark.convert(util.get_entry(title)),
        "header": title
    })
Exemplo n.º 26
0
    def render(self, context):
        output = self.nodelist.render(context)
        try:
            import markdown2
        except ImportError:
            print u"Template uses markdown2 tag but markdown2 library not found."
            raise

        md = markdown2.Markdown()
        return md.convert(output)
Exemplo n.º 27
0
def get_logs(request):
    version_expr = r'^(?P<major>\d+)_(?P<major>\d+)+_(?P<major>\d+)\.md$'
    for file in os.listdir("../versions/changelogs"):
        match = re.match(version_expr, file)
        markdowner = markdown2.Markdown()
        if match:
            major, minor, patch = match.groups()
            with open('versions/changelogs' + file) as f:
                logs[(major, minor, patch)] = markdowner.convert('\n'.join(
                    line for line in f.readlines()))
Exemplo n.º 28
0
def check(request):
    title=request.POST["q"]
    md=markdown2.Markdown()
    res=util.get_entry(title)
    if res is None:
        match=[x for x in util.list_entries() if title in x]
        print(match,file=sys.stdout)
        return render(request,"encyclopedia/search.html",{"entries": match})
    else:
        return redirect("/wiki/"+title)
Exemplo n.º 29
0
 def get_object(self, queryset=None):
     # 覆写 get_object 方法的目的是因为需要对 post 的 body 值进行渲染
     post = super(PostDetailView, self).get_object(queryset=None)
     md = markdown2.Markdown(extras=[
                                        'code-friendly', 'fenced-code-blocks', 'footnotes', 'toc'
                                    ], safe_mode=True)
     post.body = md.convert(post.body)
     if hasattr(md, 'toc_html'):
         post.toc = md.toc_html
     return post
Exemplo n.º 30
0
def random_page(request):
    entries = util.list_entries()
    entry = random.randint(0, len(entries) - 1)
    page = entries[entry]
    entry = util.get_entry(page)
    md = markdown2.Markdown()
    entry = md.convert(entry)
    return render(request, "encyclopedia/page.html", {
        "entry": entry,
    })