Created
October 1, 2011 12:17
-
-
Save ralfebert/1255969 to your computer and use it in GitHub Desktop.
Ruby String helpers: indent, to_javadoc, markdown, pygmentize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Placed in Public Domain by Ralf Ebert, 2008 | |
# | |
# THIS SOFTWARE IS PROVIDED BY Ralf Ebert ''AS IS'' AND ANY | |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL Ralf Ebert BE LIABLE FOR ANY | |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
class String | |
# Returns an indented string, all lines of string will be indented with count of chars | |
def indent(char, count) | |
(char * count) + gsub(/(\n+)/) { $1 + (char * count) } | |
end | |
# Returns string formatted as javadoc comment | |
def to_javadoc | |
"/**\n" + strip.indent(" * ", 1) + "\n */" | |
end | |
# Converts this string from markdown markup to html | |
# Requires Markdown.pl (http://daringfireball.net/projects/markdown/) | |
# to be in path as 'markdown' | |
# Download Markdown.pl and create a symbolic link, | |
# f.e. "ln -s Markdown.pl /usr/bin/markdown" | |
def markdown | |
markdown = IO.popen("markdown", "w+") | |
markdown.puts self | |
markdown.close_write | |
result = markdown.gets(nil) | |
markdown.close | |
result | |
end | |
# Converts this string to syntax highlighted html using Python pygments | |
# http://pygments.org/ | |
# Pygments installation: http://pygments.org/docs/installation/ | |
# (usually 'sudo easy_install Pygments') | |
# Call 'pygmentize -L' for list of available lexers | |
# To generate the CSS for the generated HTML: 'pygmentize -S default -f html' | |
def pygmentize(lexer) | |
pygmentize = IO.popen("pygmentize -f html -l #{lexer}", "w+") | |
pygmentize.puts self | |
pygmentize.close_write | |
result = pygmentize.gets(nil) | |
pygmentize.close | |
result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment