• dshafik

    I managed to get Pygments image drawing working by monkey patching the font stuff (ick!):

    import clipboard
    import os
    import _font_cache
    from objc_util import * 
    from pygments import highlight
    from pygments.lexers import PhpLexer
    from pygments.formatters import ImageFormatter
    from pygments.formatters.img import FontManager
    from pygments.styles import monokai
    monokai.MonokaiStyle.background_color = '#000000'
    def _get_nix_font_path(self, name, style):
        if style == 'bold' or style == 'italic':
            return _font_cache.get_font_path('%s %s' % (name, style.capitalize()))  
        elif style == '':
            return _font_cache.get_font_path(name)
    FontManager._get_nix_font_path = _get_nix_font_path
    ''' Replace Unicode newline chars with regular ones '''
    code = clipboard.get().replace(unichr(8232), os.linesep)
    png = highlight(code, PhpLexer(startinline=True), ImageFormatter(style='monokai', font_name='Source Code Pro', font_size=60, line_numbers=False, line_pad=4))
    with open('highlight.png', 'w+') as fp:
    data = NSData.dataWithContentsOfFile_('highlight.png')
    c = ObjCClass('UIPasteboard')
    pasteBoard = c.generalPasteboard()
    pasteBoard.setData_forPasteboardType_(data, 'public.png')

    Resulting in an image like so:

    Highlighted Source

    posted in Pythonista read more
  • dshafik

    @JonB I've not seen that syntax before, I assume creates a block (or some sort of scoped section) in which fp is made available and then when the block is left it is automatically closed?

    Anyhow, I do now see data in data but I still can't get it to paste in. Were you able to do that?

    posted in Pythonista read more
  • dshafik

    So, 2.0 is now out, and I've continued working on this. I've tried multiple approaches and none of them work:

    from objc_util import * 
    from pygments import highlight
    from pygments.lexers import PhpLexer
    from pygments.formatters import RtfFormatter
    code = '<?php echo "hello world"; ?>'
    rtf = highlight(code, PhpLexer(), RtfFormatter())
    fp = open('highlight.rtf', 'w+')
    data = NSData.dataWithContentsOfFile_('highlight.rtf')
    c = ObjCClass('UIPasteboard')
    pasteBoard = c.generalPasteboard()
    pasteBoard.setValue_forPasteboardType_(data, 'public.rtf')

    This one results in:

    > pasteBoard.pasteboardTypes()
    <__NSCFArray: (

    But I can't seem to paste it into say, Keynote (or a Word, etc.). Maybe this is an issue with Keynote , I guess.

    Maybe I could generate an image to insert? Thoughts?

    posted in Pythonista read more
  • dshafik

    @omz will 2.0 be a free upgrade (whenever apple approves it)? I just bought 1.5 today…

    posted in Pythonista read more
  • dshafik


    I am currently trying to replicate some of my Mac workflow to iOS, and the biggest hurdle I've run into is code highliting for keynote presentations.

    I tried copy and pasting from Safari (and chrome, Firefox , and opera) but it would simply paste as plain text into keynote — I was able to paste as rich text into a couple of other apps but none that would let me then get it into keynote as RTF.

    On Mac I use pygments to output RTF and pipe it into pbcopy and now have pygments up and running inside Pythonista successfully. However, I can't get it into the clipboard as RTF.

    It looks like in 1.6 I could use the native Obj-C clipboard stuff, but this release isn't out yet — any suggestions for how I might accomplish this in 1.5?

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!