Monday, January 25, 2021

Codye 1.1.3

Howard Oakley:

In the six years since I started to publish articles here, one feature I’ve been searching for is a good way to present source code. This article is the ultimate gamble: I’m going to explain how to do this, and demonstrate how well it works. So if it all goes horribly wrong, please don’t laugh. This demo is based on a single tool, the cheap app Codye, from the App Store, and aimed at anyone with a WordPress blog; it may well be suitable for other blogging systems too.

Codye really feels like a Catalyst app. On the other hand, I’m not aware of any native apps with this feature set. BBEdit can copy code as HTML, and you can copy and paste from Xcode to TextEdit and then export as HTML, but these methods require post-processing if you want HTML that uses CSS classes instead of raw formatting. On the other hand, Codye uses a JavaScript-based parser, which in my experience is not as accurate.

9 Comments RSS · Twitter

Looks like this is using Highlightr / Highlight.js. I use that in Shrugs for source highlighting as well, works nicely. FYI running that in JS has about 5MB RAM overhead.

https://github.com/RobertoMachorro/Highlightr

Do you need/want this as a proper Mac app?

@Helge You can even see in Howard’s example that the highlighting is weird. There are at least 4 bugs right there. AppleScript is even worse, as JavaScript doesn’t have access to the terminology. I would be more interested in something like this if it were a proper Mac app or Web page (either of which would avoid the uncanny valley) or hooked up to a real parser or compiler. Otherwise, I’d rather use unstyled text or Highlight.js directly.

Codeshot was just in indie app santa for free and has at least some of the functionality (PNG only). https://apps.apple.com/us/app/codeshot/id1541665460?mt=12

Beatrix Willius

Syntax highlighting for AppleScript is plain wrong in the Codye.

Recently I tinkered a bit around with getting html out of AppleScript. Getting style information is baked into AppleScript. Translating rtf into html wasn't too hard. Makes ugly html but I'm not writing novels in AppleScript. Result see for instance https://www.mothsoftware.com/blog_page.php?permalink=how-to-get-a-list-of-mailboxes-with-applescript-for-outlook

In MailMate I've enabled the Pygments bundle which supports AppleScript. From my cursory use so far, it seems to do a decent job parsing AppleScript, though the colours it uses for the different parts of the language don't match up with the standard ones used in Script Editor or Script Debugger. I assume this would be customisable with some CSS but haven't looked. Given this is Python-based, I wonder if it could be integrated into BBEdit to output CSS-formatted code using the script menu. (I haven't looked into this at all myself, it's just a thought.)

@Beatrix @Jolin I’m doing NSAppleScript to NSAttributedString to HTML with a little post-processing. It works pretty well and could be integrated into BBEdit using PyObjC.

All this is happening at the wrong level of abstraction. We should be able to embed text with a MIME-Type, and then the web browser should see that it's source code and ask the operating system how to style it. Then it could even make use of the user's preferred style customizations for source code, and navigation tools (like line numbers and code folding).

For platform-specific languages like AppleScript (or PowerShell), most of the people reading about that language on the web are using an OS that knows that language. You wouldn't need to add support for every language in the world to your blogging tool or your web browser. It can simply delegate.

We already do this with every other file type on the web. When you add an image, video, or audio file, we trust that the browser will ask the operating system for the best way to display it. We're not using sophisticated blogging tools that export a <table> of pixels just in case the user doesn't have a GIF decoder.

The frustrating part is that all of the technically difficult parts of this problem are already solved on my computer. The only issue is tying them all together. Normally that's an area where Apple excels -- they already control Safari, Xcode, and macOS -- but improving the display of source code on the web isn't on Apple's radar (so to speak).

@mjtsai "hooked up to a real parser or compiler". That's not very realistic though, it would essentially need to embed all compilers (well, at least the lexers and parsers) around, just think about how flaky just Xcode itself is wrt those kinds of operations. It would also need to have access to the full environment (toolchain etc) of the pasted snippets, as the type of symbols often depends on the imports. I don't think more than the typical regex highlighter makes sense here.

There is another way to do this though, specifically for Apple devs. AFAIK Xcode itself puts styled/highlighted text on the pasteboard. One could use that. Not sure it makes sense.

@Helge Yes, I would be much more interested in a utility that processes information copied from Xcode, as that (like my AppleScript approach) would snapshot the correct highlighting given the compilation environment at that time.

Leave a Comment