Exemple #1
0
def colorize(txt):
    res = ""
    i = 0
    name = ""
    while i < len(txt):
        car = txt[i]
        if car in ["'", '"']:
            k = i + 1
            while k < len(txt):
                if txt[k] == car:
                    res += html.SPAN(txt[i : k + 1], Class="string")
                    i = k
                    break
                k += 1
        elif car == "#":  # comment
            res += html.SPAN(txt[i:], Class="comment")
            break
        elif car in string.ascii_letters + "_":
            name += car
        elif car in string.digits and name:
            name += car
        else:
            if name:
                if re.search(kw_pattern, name):
                    res += html.SPAN(name, Class="keyword")
                elif re.search(bf_pattern, name):
                    res += html.SPAN(name, Class="keyword")
                else:
                    res += name
                name = ""
            res += car
        i += 1
    res += name
    return res
Exemple #2
0
def highlight(txt):
    res = html.PRE()
    i = 0
    name = ''
    while i < len(txt):
        car = txt[i]
        if car in ["'", '"']:
            found_match = False
            k = i + 1
            while k < len(txt):
                if txt[k] == car:
                    nb_as = 0
                    j = k - 1
                    while True:
                        if txt[j] == '\\':
                            nb_as += 1
                            j -= 1
                        else:
                            break
                    if nb_as % 2 == 0:
                        res <= name + html.SPAN(escape(txt[i:k + 1]),
                            Class="python-string")
                        i = k
                        name = ''
                        found_match = True
                        break
                k += 1
            if not found_match:
                name += car
        elif car == '#': # comment
            end = txt.find('\n', i)
            if end== -1:
                res <= html.SPAN(escape(txt[i:]), Class="python-comment")
                break
            else:
                res <= html.SPAN(escape(txt[i:end]), Class="python-comment")
                i = end-1
        elif car in letters:
            name += car
        elif car in digits and name:
            name += car
        else:
            if name:
                if re.search(kw_pattern,name):
                    res <= html.SPAN(name, Class="python-keyword")
                elif re.search(bf_pattern,name):
                    res <= html.SPAN(name, Class="python-builtin")
                else:
                    res <= name
                name = ''
            res <= car
        i += 1
    res <= name
    return res
Exemple #3
0
def highlight(txt,
              string_color="blue",
              comment_color="green",
              keyword_color="purple"):
    res = html.PRE()
    i = 0
    name = ''
    while i < len(txt):
        car = txt[i]
        if car in ["'", '"']:
            k = i + 1
            while k < len(txt):
                if txt[k] == car:
                    nb_as = 0
                    j = k - 1
                    while True:
                        if txt[j] == '\\':
                            nb_as += 1
                            j -= 1
                        else:
                            break
                    if nb_as % 2 == 0:
                        res <= name + html.SPAN(txt[i:k + 1],
                                                style=dict(color=string_color))
                        i = k
                        name = ''
                        break
                k += 1
        elif car == '#':  # comment
            end = txt.find('\n', i)
            if end == -1:
                res <= html.SPAN(txt[i:], style=dict(color=comment_color))
                break
            else:
                res <= html.SPAN(txt[i:end], style=dict(color=comment_color))
                i = end - 1
        elif car in letters:
            name += car
        elif car in digits and name:
            name += car
        else:
            if name:
                if re.search(kw_pattern, name):
                    res <= html.SPAN(name, style=dict(color=keyword_color))
                elif re.search(bf_pattern, name):
                    res <= html.SPAN(name, style=dict(color=keyword_color))
                else:
                    res <= name
                name = ''
            res <= car
        i += 1
    res <= name
    return res
Exemple #4
0
def highlight(txt, string_color="blue", comment_color="green",
    keyword_color="purple"):
    res = html.PRE()
    i = 0
    name = ''
    while i<len(txt):
        car = txt[i]
        if car in ["'",'"']:
            k = i+1
            while k<len(txt):
                if txt[k]==car:
                    nb_as = 0
                    j = k-1
                    while True:
                        if txt[j]=='\\':
                            nb_as+=1
                            j -= 1
                        else:
                            break
                    if nb_as % 2 == 0:
                        res <= name+html.SPAN(txt[i:k+1],
                            style=dict(color=string_color))
                        i = k
                        name = ''
                        break
                k += 1
        elif car == '#': # comment
            end = txt.find('\n', i)
            if end== -1:
                res <= html.SPAN(txt[i:],style=dict(color=comment_color))
                break
            else:
                res <= html.SPAN(txt[i:end],style=dict(color=comment_color))
                i = end-1
        elif car in letters:
            name += car
        elif car in digits and name:
            name += car
        else:
            if name:
                if re.search(kw_pattern,name):
                    res <= html.SPAN(name,style=dict(color=keyword_color))
                elif re.search(bf_pattern,name):
                    res <= html.SPAN(name,style=dict(color=keyword_color))
                else:
                    res <= name
                name = ''
            res <= car
        i += 1
    res <= name
    return res
Exemple #5
0
def search(pattern, string, flags=0):
    """Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found."""
    if _jsre._is_valid(pattern):
        return _jsre.search(pattern, string, flags)
    else:
        return _pyre().search(pattern, string, flags)
Exemple #6
0
def search(pattern, string, flags=0):
    """Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found."""
    if _jsre._is_valid(pattern):
        return _jsre.search(pattern, string, flags)
    else:
        return _pyre().search(pattern, string, flags)
Exemple #7
0
def mark(src):

    global refs
    t0 = time.time()
    refs = {}
    # split source in sections
    # sections can be :
    # - a block-level HTML element (markdown syntax will not be processed)
    # - a script
    # - a span-level HTML tag (markdown syntax will be processed)
    # - a code block

    # normalise line feeds
    src = src.replace("\r\n", "\n")

    # lines followed by dashes
    src = re.sub(r"(.*?)\n=+\n", "\n# \\1\n", src)
    src = re.sub(r"(.*?)\n-+\n", "\n## \\1\n", src)

    lines = src.split("\n") + [""]

    i = bq = 0
    ul = ol = 0

    while i < len(lines):

        # enclose lines starting by > in a blockquote
        if lines[i].startswith(">"):
            nb = 1
            while nb < len(lines[i]) and lines[i][nb] == ">":
                nb += 1
            lines[i] = lines[i][nb:]
            if nb > bq:
                lines.insert(i, "<blockquote>" * (nb - bq))
                i += 1
                bq = nb
            elif nb < bq:
                lines.insert(i, "</blockquote>" * (bq - nb))
                i += 1
                bq = nb
        elif bq > 0:
            lines.insert(i, "</blockquote>" * bq)
            i += 1
            bq = 0

        # unordered lists
        if (
            lines[i].strip()
            and lines[i].lstrip()[0] in "-+*"
            and len(lines[i].lstrip()) > 1
            and lines[i].lstrip()[1] == " "
            and (i == 0 or ul or not lines[i - 1].strip())
        ):
            # line indentation indicates nesting level
            nb = 1 + len(lines[i]) - len(lines[i].lstrip())
            lines[i] = "<li>" + lines[i][nb:]
            if nb > ul:
                lines.insert(i, "<ul>" * (nb - ul))
                i += 1
            elif nb < ul:
                lines.insert(i, "</ul>" * (ul - nb))
                i += 1
            ul = nb
        elif ul and not lines[i].strip():
            if i < len(lines) - 1 and lines[i + 1].strip() and not lines[i + 1].startswith(" "):
                nline = lines[i + 1].lstrip()
                if nline[0] in "-+*" and len(nline) > 1 and nline[1] == " ":
                    pass
                else:
                    lines.insert(i, "</ul>" * ul)
                    i += 1
                    ul = 0

        # ordered lists
        mo = re.search(r"^(\d+\.)", lines[i])
        if mo:
            if not ol:
                lines.insert(i, "<ol>")
                i += 1
            lines[i] = "<li>" + lines[i][len(mo.groups()[0]) :]
            ol = 1
        elif (
            ol
            and not lines[i].strip()
            and i < len(lines) - 1
            and not lines[i + 1].startswith(" ")
            and not re.search(r"^(\d+\.)", lines[i + 1])
        ):
            lines.insert(i, "</ol>")
            i += 1
            ol = 0

        i += 1

    if ul:
        lines.append("</ul>" * ul)
    if ol:
        lines.append("</ol>" * ol)
    if bq:
        lines.append("</blockquote>" * bq)

    t1 = time.time()
    # print('part 1', t1-t0)
    sections = []
    scripts = []
    section = Marked()

    i = 0
    while i < len(lines):
        line = lines[i]
        if line.strip() and line.startswith("    "):
            if isinstance(section, Marked) and section.line:
                sections.append(section)
            section = CodeBlock(line[4:])
            j = i + 1
            while j < len(lines) and lines[j].startswith("    "):
                section.lines.append(lines[j][4:])
                j += 1
            sections.append(section)
            section = Marked()
            i = j
            continue

        elif line.strip() and line.startswith("```"):
            # fenced code blocks à la Github Flavoured Markdown
            if isinstance(section, Marked) and section.line:
                sections.append(section)
            section = CodeBlock(line)
            j = i + 1
            while j < len(lines) and not lines[j].startswith("```"):
                section.lines.append(lines[j])
                j += 1
            sections.append(section)
            section = Marked()
            i = j + 1
            continue

        elif line.lower().startswith("<script"):
            if isinstance(section, Marked) and section.line:
                sections.append(section)
                section = Marked()
            j = i + 1
            while j < len(lines):
                if lines[j].lower().startswith("</script>"):
                    scripts.append("\n".join(lines[i + 1 : j]))
                    for k in range(i, j + 1):
                        lines[k] = ""
                    break
                j += 1
            i = j
            continue

        # atext header
        elif line.startswith("#"):
            level = 1
            line = lines[i]
            while level < len(line) and line[level] == "#" and level <= 6:
                level += 1
            if not line[level + 1 :].strip():
                if level == 1:
                    i += 1
                    continue
                else:
                    lines[i] = "<H%s>%s</H%s>\n" % (level - 1, "#", level - 1)
            else:
                lines[i] = "<H%s>%s</H%s>\n" % (level, line[level + 1 :], level)

        else:
            mo = re.search(ref_pattern, line)
            if mo is not None:
                if isinstance(section, Marked) and section.line:
                    sections.append(section)
                    section = Marked()
                key = mo.groups()[0]
                value = URL(mo.groups()[1])
                refs[key.lower()] = value
            else:
                if not line.strip():
                    line = "<p></p>"
                if section.line:
                    section.line += "\n"
                section.line += line

            i += 1
    t2 = time.time()
    # print('section 2', t2-t1)
    if isinstance(section, Marked) and section.line:
        sections.append(section)

    res = ""
    for section in sections:
        mk, _scripts = section.to_html()
        res += mk
        scripts += _scripts
    # print('end mark', time.time()-t2)
    return res, scripts
Exemple #8
0
def mark(src):

    global refs
    t0 = time.time()
    refs = {}
    # split source in sections
    # sections can be :
    # - a block-level HTML element (markdown syntax will not be processed)
    # - a script
    # - a span-level HTML tag (markdown syntax will be processed)
    # - a code block

    # normalise line feeds
    src = src.replace('\r\n', '\n')

    # lines followed by dashes
    src = re.sub(r'(.*?)\n=+\n', '\n# \\1\n', src)
    src = re.sub(r'(.*?)\n-+\n', '\n## \\1\n', src)

    lines = src.split('\n') + ['']

    i = bq = 0
    ul = ol = 0

    while i < len(lines):

        # enclose lines starting by > in a blockquote
        if lines[i].startswith('>'):
            nb = 1
            while nb < len(lines[i]) and lines[i][nb] == '>':
                nb += 1
            lines[i] = lines[i][nb:]
            if nb > bq:
                lines.insert(i, '<blockquote>' * (nb - bq))
                i += 1
                bq = nb
            elif nb < bq:
                lines.insert(i, '</blockquote>' * (bq - nb))
                i += 1
                bq = nb
        elif bq > 0:
            lines.insert(i, '</blockquote>' * bq)
            i += 1
            bq = 0

        # unordered lists
        if lines[i].strip() and lines[i].lstrip()[0] in '-+*' \
            and len(lines[i].lstrip())>1 \
            and lines[i].lstrip()[1]==' ' \
            and (i==0 or ul or not lines[i-1].strip()):
            # line indentation indicates nesting level
            nb = 1 + len(lines[i]) - len(lines[i].lstrip())
            lines[i] = '<li>' + lines[i][nb:]
            if nb > ul:
                lines.insert(i, '<ul>' * (nb - ul))
                i += 1
            elif nb < ul:
                lines.insert(i, '</ul>' * (ul - nb))
                i += 1
            ul = nb
        elif ul and not lines[i].strip():
            if i<len(lines)-1 and lines[i+1].strip() \
                and not lines[i+1].startswith(' '):
                nline = lines[i + 1].lstrip()
                if nline[0] in '-+*' and len(nline) > 1 and nline[1] == ' ':
                    pass
                else:
                    lines.insert(i, '</ul>' * ul)
                    i += 1
                    ul = 0

        # ordered lists
        mo = re.search(r'^(\d+\.)', lines[i])
        if mo:
            if not ol:
                lines.insert(i, '<ol>')
                i += 1
            lines[i] = '<li>' + lines[i][len(mo.groups()[0]):]
            ol = 1
        elif ol and not lines[i].strip() and i<len(lines)-1 \
            and not lines[i+1].startswith(' ') \
            and not re.search(r'^(\d+\.)',lines[i+1]):
            lines.insert(i, '</ol>')
            i += 1
            ol = 0

        i += 1

    if ul:
        lines.append('</ul>' * ul)
    if ol:
        lines.append('</ol>' * ol)
    if bq:
        lines.append('</blockquote>' * bq)

    t1 = time.time()
    #print('part 1', t1-t0)
    sections = []
    scripts = []
    section = Marked()

    i = 0
    while i < len(lines):
        line = lines[i]
        if line.strip() and line.startswith('    '):
            if isinstance(section, Marked) and section.line:
                sections.append(section)
            section = CodeBlock(line[4:])
            j = i + 1
            while j < len(lines) and lines[j].startswith('    '):
                section.lines.append(lines[j][4:])
                j += 1
            sections.append(section)
            section = Marked()
            i = j
            continue

        elif line.strip() and line.startswith("```"):
            # fenced code blocks à la Github Flavoured Markdown
            if isinstance(section, Marked) and section.line:
                sections.append(section)
            section = CodeBlock(line)
            j = i + 1
            while j < len(lines) and not lines[j].startswith("```"):
                section.lines.append(lines[j])
                j += 1
            sections.append(section)
            section = Marked()
            i = j + 1
            continue

        elif line.lower().startswith('<script'):
            if isinstance(section, Marked) and section.line:
                sections.append(section)
                section = Marked()
            j = i + 1
            while j < len(lines):
                if lines[j].lower().startswith('</script>'):
                    scripts.append('\n'.join(lines[i + 1:j]))
                    for k in range(i, j + 1):
                        lines[k] = ''
                    break
                j += 1
            i = j
            continue

        # atext header
        elif line.startswith('#'):
            level = 1
            line = lines[i]
            while level < len(line) and line[level] == '#' and level <= 6:
                level += 1
            if not line[level + 1:].strip():
                if level == 1:
                    i += 1
                    continue
                else:
                    lines[i] = '<H%s>%s</H%s>\n' % (level - 1, '#', level - 1)
            else:
                lines[i] = '<H%s>%s</H%s>\n' % (level, line[level + 1:], level)

        else:
            mo = re.search(ref_pattern, line)
            if mo is not None:
                if isinstance(section, Marked) and section.line:
                    sections.append(section)
                    section = Marked()
                key = mo.groups()[0]
                value = URL(mo.groups()[1])
                refs[key.lower()] = value
            else:
                if not line.strip():
                    line = '<p></p>'
                if section.line:
                    section.line += '\n'
                section.line += line

            i += 1
    t2 = time.time()
    #print('section 2', t2-t1)
    if isinstance(section, Marked) and section.line:
        sections.append(section)

    res = ''
    for section in sections:
        mk, _scripts = section.to_html()
        res += mk
        scripts += _scripts
    #print('end mark', time.time()-t2)
    return res, scripts
Exemple #9
0
def mark(src):

    global refs
    refs = {}
    # split source in sections
    # sections can be :
    # - a block-level HTML element (markdown syntax will not be processed)
    # - a script
    # - a span-level HTML tag (markdown syntax will be processed)
    # - a code block

    # normalise line feeds
    src = src.replace('\r\n','\n')

    # lines followed by dashes
    src = re.sub(r'(.*?)\n=+\n', '\n# \\1\n', src)
    src = re.sub(r'(.*?)\n-+\n', '\n## \\1\n', src)

    lines = src.split('\n') + ['']

    i = bq = 0
    ul = ol = 0

    while i<len(lines):

        # enclose lines starting by > in a blockquote
        if lines[i].startswith('>'):
            nb = 1
            while nb < len(lines[i]) and lines[i][nb] == '>':
                nb += 1
            lines[i] = lines[i][nb:]
            if nb>bq:
                lines.insert(i, '<blockquote>' * (nb - bq))
                i += 1
                bq = nb
            elif nb<bq:
                lines.insert(i, '</blockquote>' * (bq - nb))
                i += 1
                bq = nb
        elif bq>0:
            lines.insert(i, '</blockquote>' * bq)
            i += 1
            bq = 0

        # unordered lists
        if (lines[i].strip() and lines[i].lstrip()[0] in '-+*'
                and len(lines[i].lstrip()) > 1
                and lines[i].lstrip()[1] == ' '
                and (i == 0 or ul or not lines[i - 1].strip())):
            # line indentation indicates nesting level
            nb = 1 + len(lines[i]) - len(lines[i].lstrip())
            lines[i] = '<li>' + lines[i][nb:]
            if nb>ul:
                lines.insert(i, '<ul>' * (nb - ul))
                i += 1
            elif nb<ul:
                lines.insert(i, '</ul>' * (ul - nb))
                i += 1
            ul = nb
        elif ul and not lines[i].strip():
            if (i < len(lines) - 1 and lines[i+1].strip()
                    and not lines[i + 1].startswith(' ')):
                nline = lines[i + 1].lstrip()
                if nline[0] in '-+*' and len(nline) > 1 and nline[1] == ' ':
                    pass
                else:
                    lines.insert(i, '</ul>' * ul)
                    i += 1
                    ul = 0

        # ordered lists
        mo = re.search(r'^(\d+\.)', lines[i])
        if mo:
            if not ol:
                lines.insert(i, '<ol>')
                i += 1
            lines[i] = '<li>' + lines[i][len(mo.groups()[0]):]
            ol = 1
        elif (ol and not lines[i].strip() and i < len(lines) - 1
                and not lines[i + 1].startswith(' ')
                and not re.search(r'^(\d+\.)', lines[i + 1])):
            lines.insert(i, '</ol>')
            i += 1
            ol = 0

        i += 1

    if ul:
        lines.append('</ul>' * ul)
    if ol:
        lines.append('</ol>' * ol)
    if bq:
        lines.append('</blockquote>' * bq)

    sections = []
    scripts = []
    section = Marked()

    i = 0
    while i < len(lines):
        line = lines[i]
        if line.strip() and line.startswith('    '):
            if isinstance(section, Marked) and section.line:
                sections.append(section)
            section = CodeBlock(line[4:])
            j = i + 1
            while j < len(lines) and lines[j].startswith('    '):
                section.lines.append(lines[j][4:])
                j += 1
            sections.append(section)
            section = Marked()
            i = j
            continue

        elif line.strip() and line.startswith("```"):
            # fenced code blocks à la Github Flavoured Markdown
            if isinstance(section, Marked) and section.line:
                sections.append(section)
            section = CodeBlock(line)
            j = i + 1
            while j < len(lines) and not lines[j].startswith("```"):
                section.lines.append(lines[j])
                j += 1
            sections.append(section)
            section = Marked()
            i = j+1
            continue

        elif line.lower().startswith('<script'):
            if isinstance(section, Marked) and section.line:
                sections.append(section)
                section = Marked()
            j = i + 1
            while j < len(lines):
                if lines[j].lower().startswith('</script>'):
                    scripts.append('\n'.join(lines[i + 1:j]))
                    for k in range(i, j + 1):
                        lines[k] = ''
                    break
                j += 1
            i = j
            continue

        # atext header
        elif line.startswith('#'):
            level = 1
            line = lines[i]
            while level < len(line) and line[level] == '#' and level <= 6:
                level += 1
            if not line[level + 1:].strip():
                if level == 1:
                    i += 1
                    continue
                else:
                    lines[i] = '<H%s>%s</H%s>\n' %(level - 1, '#', level - 1)
            else:
                lines[i] = '<H%s>%s</H%s>\n' %(level, line[level + 1:], level)

        else:
            mo = re.search(ref_pattern, line)
            if mo is not None:
                if isinstance(section, Marked) and section.line:
                    sections.append(section)
                    section = Marked()
                key = mo.groups()[0]
                value = URL(mo.groups()[1])
                refs[key.lower()] = value
            else:
                if not line.strip():
                    line = '<p></p>'
                if section.line:
                    section.line += '\n'
                section.line += line

            i += 1

    if isinstance(section, Marked) and section.line:
        sections.append(section)

    res = ''
    for section in sections:
        mk, _scripts = section.to_html()
        res += mk
        scripts += _scripts

    return res, scripts