TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Racket-embed-images-in-source-text

50 pointsby spdegabrielle5 months ago

7 comments

neilv5 months ago
He&#x27;s doing this only because Racket has the unusual feature of supporting images as &quot;self-eval&quot; values in the source code.<p>In other languages, all you&#x27;d want is the byte string (maybe BASE64-encoded, maybe with byte escape sequences), and you wouldn&#x27;t need a fancy reader extension: just put a byte string literal.<p>A variation on the byte string literal you might see is when people create a minilanguage out of strings. For example, if you want to represent a 2x pixel map image with a fairly small color map... you might just have a string literal of ASCII characters for each row, where each ASCII character maps to an RGB color (or transparent) that you specify, with a simple function to translate it to binary. Then it looks like ASCII art in your source code.<p>Vector images, OTOH, are more likely to have a textual language already (e.g., SVG), so of course you can just embed that as strings in your source file.
perihelions5 months ago
I like these kinds of ideas. I&#x27;ve spent a lot of time thinking about similar ones. Here&#x27;s my rough translation of this particular concept to Emacs:<p><pre><code> (defun imagify-buffer () (interactive) (font-lock-ensure) (save-excursion (goto-char (point-min)) (while (re-search-forward (rx &quot;(%image \&quot;&quot; (group (1+ (or alphanumeric &quot;+&quot; &quot;&#x2F;&quot; &quot;=&quot;))) &quot;\&quot;)&quot;) nil t) (when (equal &#x27;font-lock-comment-face (get-char-property (match-beginning 0) &#x27;face)) (let* ((replaced-string (match-string 0)) (base64 (match-string 1)) (image (create-image (base64-decode-string base64) nil t :scale 4.0))) (goto-char (match-beginning 0)) (kill-region (match-beginning 0) (match-end 0)) (insert-image image replaced-string)))))) (defun insert-image-thingy (filename) (interactive &quot;f&quot;) (let* ((s (with-temp-buffer (set-buffer-multibyte nil) (insert-file-contents-literally filename) (buffer-substring-no-properties (point-min) (point-max)))) (base64 (base64-encode-string s t)) (thingy (format &quot;;; (%%image \&quot;%s\&quot;)&quot; base64))) (insert thingy) (imagify-buffer))) (add-hook &#x27;emacs-lisp-mode-hook #&#x27;imagify-buffer) (add-hook &#x27;lisp-interaction-mode-hook #&#x27;imagify-buffer) (imagify-buffer) ;; (%image &quot;PHN2ZyBoZWlnaHQ9IjE4IiB2aWV3Qm94PSI0IDQgMTg4IDE4OCIgd2lkdGg9IjE4IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIGQ9Im00IDRoMTg4djE4OGgtMTg4eiIgZmlsbD0iI2Y2MCIvPjxwYXRoIGQ9Im03My4yNTIxNzU2IDQ1LjAxIDIyLjc0NzgyNDQgNDcuMzkxMzAwODMgMjIuNzQ3ODI0NC00Ny4zOTEzMDA4M2gxOS41NjU2OTYzMWwtMzQuMzIzNTIwNzEgNjQuNDg2NjE0Njh2NDEuNDkzMzg1MzJoLTE1Ljk4di00MS40OTMzODUzMmwtMzQuMzIzNTIwNzEtNjQuNDg2NjE0Njh6IiBmaWxsPSIjZmZmIi8+PC9zdmc+&quot;) </code></pre> This adaptation hooks onto an Emacs language mode—Emacs Lisp mode, in this snippet—and searches for, and translates, a regex syntax (%image) appearing in the comments section. This piggybacks on the language mode&#x27;s own parser (the &#x27;font-lock-comment-face thingy). The image-handling part is core Emacs functionality; like DrRacket it does support these things natively.
IshKebab5 months ago
I really wish IDEs would support showing images inline in code. I can&#x27;t count the number of times I&#x27;ve wanted to include a nice diagram in a comment but I instead had to resort to describing the diagram with words, or a shitty ASCII art diagram.<p>There&#x27;s a very long standing VSCode issue for this.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;vscode&#x2F;issues&#x2F;3220">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;vscode&#x2F;issues&#x2F;3220</a>
WiggleGuy5 months ago
Hey, that&#x27;s my former professor! Hi Shriram!
diath5 months ago
On a related note, one of the features in TempleOS was images embedded in source code along with an image editor within the code editor: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;shorts&#x2F;196Dv3gFslU" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;shorts&#x2F;196Dv3gFslU</a>
Tempest19815 months ago
Seems clean. Does the Racket IDE already support images?
评论 #42541675 未加载
adammarples5 months ago
I read the readme, I read all the links. I didn&#x27;t see a single image. Why are developers like this.
评论 #42545025 未加载