コード例 #1
0
    # Get user input
    print(
        'Use CTRL+C, CTRL+D or simply type "EOF" on a single line to stop text input'
    )
    text = []
    try:
        while True:
            line = input('skcode> ')
            if line == 'EOF':
                break
            text.append(line)
    except (KeyboardInterrupt, EOFError):
        pass
    text = '\n'.join(text)

    # Parse text
    ast = parse_skcode(text)

    # Dump parsing info
    print('----- Input text -----')
    print(text)
    print('----- Document tree -----')
    debug_print_ast(ast)
    print('----- HTML output -----')
    print(render_to_html(ast))
    print('----- TEXT output -----')
    print(render_to_text(ast))

    # End of script
    input('Press enter to exit.')
コード例 #2
0
ファイル: utils.py プロジェクト: TamiaLab/carnetdumaker
def render_document(input_text,
                    allow_titles=False,
                    allow_code_blocks=False,
                    allow_alerts_box=False,
                    allow_text_formating=False,
                    allow_text_extra=False,
                    allow_text_alignments=False,
                    allow_text_directions=False,
                    allow_text_modifiers=False,
                    allow_text_colors=False,

                    allow_spoilers=False,
                    allow_figures=False,
                    allow_lists=False,
                    allow_todo_lists=False,
                    allow_definition_lists=False,
                    allow_tables=False,

                    allow_quotes=False,
                    allow_footnotes=False,
                    allow_acronyms=False,
                    allow_links=False,
                    allow_medias=False,

                    allow_cdm_extra=False,

                    force_nofollow=True,
                    preview_mode=False,
                    hard_newline=False,
                    render_text_version=False,
                    render_extra_dict=False,
                    merge_footnotes_html=False,
                    merge_footnotes_text=False,
                    make_auto_paragraphs=True):
    """
    Render the given document as HTML, text (if requested) and output extra runtime information.
    :param input_text: The input document text (not safe).
    """

    # Shortcut for empty documents
    input_text = input_text.strip()
    if not input_text:
        return '', '', {
            'footnotes_html': '',
            'footnotes_text': '',
            'summary_html': '',
            'summary_text': '',
        }

    # Filter tags according to permissions
    allowed_tags = {}
    copy_tags_if_allowed(('h1', 'h2', 'h3',
                          'h4', 'h5', 'h6'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_titles)
    copy_tags_if_allowed(('code', 'python', 'cpp',
                          'java', 'html', 'php'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_code_blocks)
    copy_tags_if_allowed(('alert', 'error', 'danger',
                          'warning', 'info', 'success',
                          'note', 'question'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_alerts_box)
    copy_tags_if_allowed(('b', 'bold', 'strong',
                          'i', 'italic', 'em',
                          's', 'strike', 'del',
                          'u', 'underline', 'ins',
                          'sub', 'sup', 'pre',
                          'icode', 'kbd', 'keyboard',
                          'glow', 'highlight', 'mark',
                          'cite', 'not'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_formating)
    copy_tags_if_allowed(('nobbc', 'noparse', 'hr', 'br'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_extra)
    copy_tags_if_allowed(('center', 'left', 'right'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_alignments)
    copy_tags_if_allowed(('bdo', 'ltr', 'rtl'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_directions)
    copy_tags_if_allowed(('lowercase', 'uppercase', 'capitalize'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_modifiers)
    copy_tags_if_allowed(('color', 'black', 'blue',
                          'gray', 'green', 'orange',
                          'purple', 'red', 'white', 'yellow'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_text_colors)
    copy_tags_if_allowed(('spoiler', 'hide', 'ispoiler'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_spoilers)
    copy_tags_if_allowed(('figure', 'figcaption'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_figures)
    copy_tags_if_allowed(('list', 'ul', 'ol', 'li'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_lists)
    copy_tags_if_allowed(('todolist', 'task'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_todo_lists)
    copy_tags_if_allowed(('dl', 'dt', 'dd'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_definition_lists)
    copy_tags_if_allowed(('table', 'tr', 'th', 'td'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_tables)
    copy_tags_if_allowed(('quote', 'blockquote'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_quotes)
    copy_tags_if_allowed(('footnote', 'fn', 'fnref'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_footnotes)
    copy_tags_if_allowed(('abbr', 'acronym'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_acronyms)
    copy_tags_if_allowed(('anchor', 'goto', 'url', 'link', 'email'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_links)
    copy_tags_if_allowed(('img', 'youtube'), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_medias)
    # copy_tags_if_allowed(('', ''), DEFAULT_RECOGNIZED_TAGS, allowed_tags, allow_cdm_extra)

    # Handle preview_mode and hard_newline options
    erroneous_text_opts = ErroneousTextTagOptions() if preview_mode else TextTagOptions()
    newlines_opts = HardNewlineTagOptions() if hard_newline else NewlineTagOptions()

    # Parse the document
    document = parse_skcode(input_text,
                            allow_tagvalue_attr=True,
                            allow_self_closing_tags=True,
                            erroneous_text_node_opts=erroneous_text_opts,
                            newline_node_opts=newlines_opts,
                            drop_unrecognized=False,
                            texturize_unclosed_tags=False)

    # Setup smileys and cosmetics replacement
    def _base_url(filename):
        return static(EMOTICONS_IMG_DIR + filename)
    setup_cosmetics_replacement(document)
    setup_smileys_replacement(document, _base_url)

    # Setup absolute URL conversion
    if RELATIVE_URL_BASE:
        setup_relative_urls_conversion(document, RELATIVE_URL_BASE)

    # Make paragraphs
    if make_auto_paragraphs:
        make_paragraphs(document)

    # Make auto titles IDs
    make_auto_title_ids(document)

    # Handle footnotes
    if allow_footnotes and (render_extra_dict or merge_footnotes_html or merge_footnotes_text):
        footnotes = extract_footnotes(document)
        footnotes_output_html = render_footnotes_html(footnotes)
        footnotes_output_text = render_footnotes_text(footnotes) if render_text_version else ''
    else:
        footnotes_output_html = ''
        footnotes_output_text = ''

    # Extra information dictionary
    if render_extra_dict:

        # Extract all titles
        titles = extract_titles(document)

        # Turn the titles list into a hierarchy
        titles_hierarchy = make_titles_hierarchy(titles)

        # Render titles hierarchy
        titles_summary_output_html = render_titles_hierarchy_html(titles_hierarchy)
        titles_summary_output_text = render_titles_hierarchy_text(titles_hierarchy) if render_text_version else ''

    else:
        titles_summary_output_html = ''
        titles_summary_output_text = ''

    # Render the document
    content_html = render_to_html(document, force_rel_nofollow=force_nofollow)
    content_text = render_to_text(document) if render_text_version else ''

    # Merge footnotes if requested
    if merge_footnotes_html and footnotes_output_html:
        content_html += '<hr>\n' + footnotes_output_html
    if merge_footnotes_text and footnotes_output_text:
        content_text += '----------\n\n' + footnotes_output_text

    # Return the result
    return content_html, content_text, {
        'footnotes_html': footnotes_output_html,
        'footnotes_text': footnotes_output_text,
        'summary_html': titles_summary_output_html,
        'summary_text': titles_summary_output_text,
    }
コード例 #3
0
ファイル: skterm.py プロジェクト: TamiaLab/PySkCode
    print('-- SkCode testing terminal --')

    # Get user input
    print('Use CTRL+C, CTRL+D or simply type "EOF" on a single line to stop text input')
    text = []
    try:
        while True:
            line = input('skcode> ')
            if line == 'EOF':
                break
            text.append(line)
    except (KeyboardInterrupt, EOFError):
        pass
    text = '\n'.join(text)

    # Parse text
    ast = parse_skcode(text)

    # Dump parsing info
    print('----- Input text -----')
    print(text)
    print('----- Document tree -----')
    debug_print_ast(ast)
    print('----- HTML output -----')
    print(render_to_html(ast))
    print('----- TEXT output -----')
    print(render_to_text(ast))

    # End of script
    input('Press enter to exit.')
コード例 #4
0
def home_page(request,
              template_name='home/home.html',
              test_input_form=TestSkCodeInputForm,
              extra_context=None):
    """
    PySkCode tester home page with form for testing the parser.
    :param request: The current request.
    :param template_name: The template name to be used.
    :param test_input_form: The test input form class to be used.
    :param extra_context: Any extra template context information.
    :return: TemplateResponse
    """

    # Default values
    output_content_html = ''
    output_content_text = ''
    summary_content_html = ''
    summary_content_text = ''
    footnotes_content_html = ''
    footnotes_content_text = ''
    document_has_errors = False

    # Handle the form
    if request.method == "POST":
        form = test_input_form(request.POST, request.FILES)
        if form.is_valid():

            # Parse the input text
            newline_node_cls = HardNewlineTreeNode if form.cleaned_data['hard_newline'] else NewlineTreeNode
            html_error_template = DEFAULT_ERROR_HTML_TEMPLATE if form.cleaned_data['preview_mode'] else SUPPRESS_ERROR_HTML_TEMPLATE
            document = parse_skcode(form.cleaned_data['content'],
                                    allow_tagvalue_attr=form.cleaned_data['allow_tagvalue_attr'],
                                    allow_self_closing_tags=form.cleaned_data['allow_self_closing_tags'],
                                    mark_unclosed_tags_as_erroneous=form.cleaned_data['mark_unclosed_tags'],
                                    newline_node_cls=newline_node_cls)
            document_has_errors = document.has_errors()

            # Handle smileys and cosmetics
            if form.cleaned_data['replace_cosmetics']:
                setup_cosmetics_replacement(document)
            if form.cleaned_data['replace_smileys']:

                def _base_url(filename):
                    return static('images/smileys/' + filename)
                setup_smileys_replacement(document, _base_url)

            # Handle relative urls
            if form.cleaned_data['convert_relative_url_to_absolute']:
                current_site = get_current_site(request)
                setup_relative_urls_conversion(document, 'http://%s/' % current_site.domain)

            # Get requested render mode
            rendering_mode = form.cleaned_data['rendering_mode']

            # Apply paragraph utilities
            if form.cleaned_data['make_paragraphs']:
                make_paragraphs(document)

            # Apply footnotes utilities
            if form.cleaned_data['render_footnotes_html']:

                # Extract all footnotes
                footnotes = extract_footnotes(document)

                # Render all footnotes
                if rendering_mode == RENDERING_MODE_HTML:
                    footnotes_content_html = render_footnotes_html(footnotes,
                                                                   html_error_template=html_error_template)
                elif rendering_mode == RENDERING_MODE_TEXT:
                    footnotes_content_text = render_footnotes_text(footnotes)

            # Apply titles utilities (part 1 of 2)
            if form.cleaned_data['make_auto_title_ids']:
                make_auto_title_ids(document)

            # Apply titles utilities (part 2 of 2)
            if form.cleaned_data['extract_titles']:

                # Extract all titles
                titles = extract_titles(document)

                # Turn the titles list into a hierarchy
                titles_hierarchy = list(make_titles_hierarchy(titles))

                # Render the output
                if rendering_mode == RENDERING_MODE_HTML:
                    summary_content_html = render_titles_hierarchy_html(titles_hierarchy)
                elif rendering_mode == RENDERING_MODE_TEXT:
                    summary_content_text = render_titles_hierarchy_text(titles_hierarchy)

            # Render the document
            if rendering_mode == RENDERING_MODE_HTML:
                output_content_html = render_to_html(document, html_error_template=html_error_template)
            elif rendering_mode == RENDERING_MODE_TEXT:
                output_content_text = render_to_text(document)

    else:
        form = test_input_form()

    print(output_content_html)

    # Render the template
    context = {
        'form': form,
        'document_has_errors': document_has_errors,
        'output_content_html': output_content_html,
        'output_content_text': output_content_text,
        'summary_content_html': summary_content_html,
        'summary_content_text': summary_content_text,
        'footnotes_content_html': footnotes_content_html,
        'footnotes_content_text': footnotes_content_text,
        'title': _('Home page'),
    }
    if extra_context is not None:
        context.update(extra_context)

    return TemplateResponse(request, template_name, context)