Skip to content

Instantly share code, notes, and snippets.

@0cch
Last active August 29, 2015 14:23
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 0cch/775e4a8a94be175cae9c to your computer and use it in GitHub Desktop.
Save 0cch/775e4a8a94be175cae9c to your computer and use it in GitHub Desktop.
def string_rsub(str, pattern, replacement)
last_match = nil
while true
pos = last_match.nil? ? 0 : last_match.end(0)
m = pattern.match(str, pos)
break if m.nil?
ast_match = m
end
last_match ? (last_match.pre_match + replacement + last_match.post_match) : str
end
def add_code_tag(code)
# Add nested <code> tags to code blocks
code = code.sub(/<pre>\n*/,'<pre><code class="language-' + @lang.to_s.gsub("+", "-") + '" data-lang="' + @lang.to_s + '">')
code = string_rsub(code, /\n*<\/pre>/,"</code></pre>")
code.strip
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment