def format_rtf(t):
    tokensource = list(TextLexer().get_tokens(t))
    fmt = RtfFormatter()
    buf = StringIO()
    fmt.format(tokensource, buf)
    result = buf.getvalue()
    buf.close()
    return result
Ejemplo n.º 2
0
 def format_rtf(self, t):
     tokensource = list(TextLexer().get_tokens(t))
     fmt = RtfFormatter()
     buf = StringIO()
     fmt.format(tokensource, buf)
     result = buf.getvalue()
     buf.close()
     return result
Ejemplo n.º 3
0
def paste_rtf_all(id):
    data = query_db("select * from pastes where id = ?", [id], one=True)
    try:
        Lexer = get_lexer_by_name(data["syntaxhighlight"].lower())
    except:
        Lexer = get_lexer_by_name("python")
    return Response(highlight(data["text"], Lexer, RtfFormatter()),
                    mimetype="application/rtf")
Ejemplo n.º 4
0
    def __str__(self) -> str:
        try:
            from pygments import highlight
            from pygments.formatters import RtfFormatter
            from pygments.lexers import PythonLexer
        except ImportError:
            raise ImportError("Pygments is required for RTF output.")

        out = str(self.code_block)
        if self.advertise:
            out += "\n\n" + Advertisement().text()
        if self.session_info:
            out += "\n\n" + str(SessionInfo())
        return highlight(out, PythonLexer(), RtfFormatter())
Ejemplo n.º 5
0
def harald_pdfw():
    f = FormParser.new(request.form)
    id = f.get('id', '0')
    data = query_db("select * from pastes where id = ?", [id], one=True)
    try:
        Lexer = get_lexer_by_name(data["syntaxhighlight"].lower())
    except:
        Lexer = get_lexer_by_name("python")
    with open(os.path.join("static", "pdf", id + ".rtf"), "w") as rtfc:
        rtfc.write(highlight(data["text"], Lexer, RtfFormatter()))

    if not os.path.isfile("rtf2pdf.sh"):
        return "Fehler! Harald nicht gefunden."
    convert(os.path.join("static", "pdf", id + ".rtf"),
            os.path.join("static", "pdf", id + ".pdf"))
    return "Ready"
Ejemplo n.º 6
0
 def update(self, s):
     retval = None
     # find format
     fmt = 'string'
     if isinstance(s, NSAttributedString):
         fmt = 'NSAttributedString'
     elif isinstance(s, NSData):
         fmt = 'rtf'
     # we only do highlighting if string is supplied
     if fmt == 'string':
         # format the output
         try:
             lexer = guess_lexer(
                 s).__class__ if self.lexer is True else self.lexer
         except pygments.util.ClassNotFound:
             lexer = None
         if lexer is not None:
             rtf = highlight(
                 s,
                 lexer(),
                 RtfFormatter(
                     style=self.style,
                     fontface=self.font,
                     fontsize=self.fontsize *
                     2  # *2 as it wants half points
                 )).encode('utf-8')
             data = NSData.dataWithBytes_length_(rtf, len(rtf))
             retval = self.update(data)
         else:
             # fallback - the string unformatted
             retval = NSAttributedString.alloc().initWithString_(s)
             if self.sel is not None:
                 self.sel(retval)
     elif fmt == 'rtf':
         a = NSAttributedString.alloc().initWithRTF_documentAttributes_(
             s, None)
         retval = a[0]
         if self.sel is not None:
             self.sel(a[0])
     elif fmt == 'NSAttributedString':
         retval = s
         if self.sel is not None:
             self.sel(s)
     else:
         raise ValueError('unsupported format; %s' % fmt)
     return retval
Ejemplo n.º 7
0
def output_rtf(f, code, lexer):
    # Font size is specified in HALF POINTS; 28 = 14pt
    formatter = RtfFormatter(style=MongoDark,
                             fontface="fira mono",
                             fontsize=37)
    f.write(highlight(code, lexer, formatter))
Ejemplo n.º 8
0
                    'w') as write_file_html, open(p_rtf + '.rtf',
                                                  'w') as write_file_rtf:
                try:
                    source = read_file.read()
                except:
                    source = 'None'
                print(full_path)
                heading_html = '<p style="font-size:14pt;font-family:sans-serif"><strong>' + full_path + '</strong></p>'
                code_html = heading_html + highlight(
                    source, lexer, HtmlFormatter(noclasses=True))
                full_file_content += code_html + '<br>'
                write_file_html.write(code_html)

                heading_rtf = full_path + '\n'
                code_rtf = highlight(heading_rtf + source, lexer,
                                     RtfFormatter())
                write_file_rtf.write(code_rtf)

with open('full_code.html', 'w') as final:
    final.write(full_file_content)

api = cloudconvert.Api(
    'ovnSmHPCubqsa2n4mJRNFUDXbe6MaAdr9qQOD0A7zOPbHryiOM2wafxwrWUX4Z5bRt1VJmvWujKIQsuQxTNhBw'
)

output_format = "rtf"

process = api.convert({
    "inputformat": "html",
    "outputformat": output_format,
    "input": "upload",
Ejemplo n.º 9
0
# https://forum.omz-software.com/topic/2521/rich-text-clipboard/12

from objc_util import *
from pygments import highlight
from pygments.lexers import PhpLexer
from pygments.formatters import RtfFormatter

p = ObjCClass('UIPasteboard').generalPasteboard()
code = b'<?php echo "hello world"; ?>'
rtf = highlight(code, PhpLexer(), RtfFormatter())

rtf_data = ns(bytes(rtf, 'ascii'))
p.setData_forPasteboardType_(rtf_data, 'public.RTF')
Ejemplo n.º 10
0
from pygments import highlight
from pygments.lexers.c_cpp import CppLexer
from pygments.formatters import RtfFormatter
from pygments.style import Style
from pygments.styles import STYLE_MAP
import sys
import win32clipboard as clippy

# Stylizer settings
styles = list(STYLE_MAP.keys()) # Available styles
style = styles[6]
font = "Monaco"
fontsize = 24


# Get input and style it
input = str(sys.argv[1])

output = highlight(input, CppLexer(), RtfFormatter(style=style, fontface=font, fontsize=fontsize))


# Copy to clipboard
CF_RTF = clippy.RegisterClipboardFormat("Rich Text Format")

output = bytearray(output, "utf8")

clippy.OpenClipboard(0)
clippy.EmptyClipboard()
clippy.SetClipboardData(CF_RTF, output)
clippy.CloseClipboard()