strapyourself.in and flouri.sh
Syntax highlighting Mephistro admin side
Using CodePress to make writing blog entries easier
After I got Metphisto working with Ultraviolet syntax highlighting, I decided to make writing blog entries easier! I already had some experience with my TicTacToe project using CodePress for real-time syntax highlighting. From there, I was able to modify Mephisto to use syntax highlighting while I was writing my articles... the goal being to make the HTML easier to read and easier to write:
The first step was to install CodePress from codepress.org into the mephistro public directory. I ended up putting the css in /stylesheets, and the javascript in /javascripts... big mistake! Codepress likes to be kept all together (probably best in /public), and you shouldn't have to rewrite any of the paths in codepress.html or codepress.js, like I did.
The second step was to include codepress.js in your layout.
Then I made the following modifications to /app/views/admin/articles/_form.rhtml replacing
only the dd tag that defines the "body" textarea:
<dd><%= form.text_area :body, :class => 'fat codepress html linenumbers-off', :rows => 25 %> <br/><%= check_box_tag "enable_codepress", "1", true, :onchange => "article_body.toggleEditor();" %> Use codepress for syntax highlighting <%= hidden_field_tag "article[body_hidden]", @article.body, :id => "article_body_hidden" %> </dd>
Just changing the class of the text_area is enough to active codepress... pretty cool huh? The
checkbox is cool too, allowing you to turn codepress on/off on the fly.
Unfortunately, I had to do some craziness with a hidden field. Codepress does not submit the edited
data with the form, like you'd expect. Because it creates an design-mode iframe, the browser does not
submit your code the way you want it to. However, it provides an object on which you can call a method
to get the unformatted code. We can copy the code into the hidden field with an onsubmit
in /app/views/admin/articles/new.rhtml and
/app/views/admin/articles/edit.rhtml by adding an :onsubmit option to the form like so:
{:id => 'article-form', :multipart => true, :onsubmit => "document.getElementById('article_body_hidden').value = article_body.getCode();"}
Of course, we've now got a hidden field named article[body_hidden] which contains
the actual body, rather than the regular field article[body], so we need to deal with that somewhere.
I chose /app/models/article.rb, adding the following public method:
def body_hidden=(newval) self.body = newval end
This is still mostly untested, but I'm really enjoying it so far. Uploading an asset inline might not submit the body of the article anymore, so you'll have to a similar piece of javascript to copy the value from the codepress object or lose your changes when you upload an image.
Ultraviolet syntax highlighting in Mephisto
Being a mac rails user, I love textmate. Naturally, I went crazy when I found out that Ultraviolet is a syntax highlighting gem for ruby that reads textmate theme and textmate syntax files to create xhtml highlighted code. After using this in my tictactoe project, I integrated it into mephisto. Because I wanted to use the filter:code tag that's normally used by coderay, I did this by deleting all the files in the filtered_column_code_macro/lib plugin except code_macro.rb, which I replaced as follows
require 'uv' class CodeMacro < FilteredColumn::Macros::Base def self.filter(attributes, inner_text = '', text = '') lang = attributes.delete(:lang) || "ruby_on_rails" theme = attributes.delete(:theme) || "cobalt" begin Uv.parse( inner_text, "xhtml", lang, false, theme) rescue RAILS_DEFAULT_LOGGER.warn "UltraViolet Error: #{$!.message}" RAILS_DEFAULT_LOGGER.debug $!.backtrace.join("\n") end end end
Then I copied in the xhtml CSS files for Ultraviolet xhtml rendering (as shown in the UV instructions) into my stylesheets directory. I included the themes I liked into my layout.liquid as follows:
<link rel="stylesheet" href="/stylesheets/uv/blackboard.css" type="text/css" media="screen" /> <link rel="stylesheet" href="/stylesheets/uv/dawn.css" type="text/css" media="screen" /> <link rel="stylesheet" href="/stylesheets/uv/cobalt.css" type="text/css" media="screen" />
Finally, you have to remove a reference to 'include "coderay"' in environment.rb. For each code block in your blog, you can change the theme (there's about 12 included themes):
import java.net; public class Bob extends BobParent implements Bobliness { private final int SOMETHING=0; public static void main(String[] args) { System.out.println("Hello world!" + 5.5) } }
UV comes with syntax highlightinb for 158 languages:
actionscript, active4d_html, active4d_ini, active4d_library, active4d, ada, antlr, apache, applescript, asp, asp_vb.net, bibtex, blog_html, blog_markdown, blog_textile, blog_text, build, bulletin_board, cake, camlp4, cm, coldfusion, context_free, css_experimental, css, cs, csv, c, c++, diff, dokuwiki, dot, doxygen, d, dylan, eiffel, erlang, fortran, f-script, fxscript, greasemonkey, gri, groovy, gtdalt, gtd, haml, haskell, html-asp, html_django, html_for_asp.net, html_mason, html_rails, html, html_tcl, icalendar, inform, ini, installer_distribution_script, io, javaproperties, javascript_+_prototype_bracketed, javascript_+_prototype, javascript, java, jquery_javascript, json, languagedefinition, latex_beamer, latex_log, latex_memoir, latex, lexflex, lighttpd, lilypond, lisp, literate_haskell, logo, logtalk, lua, macports_portfile, mail, makefile, man, markdown, mediawiki, mel, mips, mod_perl, modula-3, moinmoin, mootools, movable_type, m, multimarkdown, objective-c, objective-c++, ocamllex, ocaml, ocamlyacc, opengl, pascal, perl, php, plain_text, pmwiki, postscript, processing, prolog, property_list, python_django, python, qmake_project, qt_c++, quake3_config, ragel, r_console, rd_r_documentation, regexp, regular_expressions_oniguruma, regular_expressions_python, release_notes, remind, restructuredtext, rez, r, ruby_experimental, ruby_on_rails, ruby, s5, scheme, scilab, setext, shell-unix-generic, slate, smarty, sql_rails, sql, ssh-config, standard_ml, strings_file, subversion_commit_message, sweave, swig, tcl, template_toolkit, tex_math, tex, textile, tsv, twiki, txt2tags, vectorscript, xhtml_1.0, xml_strict, xml, xsl, yaml, yui_javascript