Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active September 14, 2022 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/d0e07b600d5a90e15cb5384a61791487 to your computer and use it in GitHub Desktop.
Save JoshCheek/d0e07b600d5a90e15cb5384a61791487 to your computer and use it in GitHub Desktop.
Evaluating and updating annotations in ruby code in markdown files
require 'redcarpet' # gem
require 'open3' # stdlib
class SibRenderer < Redcarpet::Render::Safe
def block_code(code, language)
if language == "ruby"
code, _status = Open3.capture2("seeing_is_believing", "--xmpfilter-style", stdin_data: code)
end
super code, language # prob what you really want, see somefile.correctly_escaped.html
%(<pre><code class="#{language}">\n#{code}</code></pre>) # better for this example, see somefile.from_example.html
end
end
markdown = Redcarpet::Markdown.new(SibRenderer.new, fenced_code_blocks: true)
puts markdown.render File.read ARGV.first
<h1>Here is some ruby:</h1>
<pre><code>&quot;abc&quot;
.upcase # =&gt; &quot;ABC&quot;
.chars # =&gt; [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]
.join(&quot;&#x2F;&quot;) # =&gt; &quot;A&#x2F;B&#x2F;C&quot;
</code></pre>
<h1>Here is some ruby:</h1>
<pre><code class="ruby">
"abc"
.upcase # => "ABC"
.chars # => ["A", "B", "C"]
.join("/") # => "A/B/C"
</code></pre>

Here is some ruby:

"abc"
  .upcase    # =>
  .chars     # =>
  .join("/") # =>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment