Thursday, July 25, 2013

FrankenCode with Rails and Xemark

I've recently created a markup language called xemark. You can find the repository here or the Backus Naur Form grammar here

I've been using it to generate my github pages site which as of the time of this writing is about 75% done.

One of the beautiful things about xemark is that it output's to the console / stdout. Which means you can pipe or direct it's output anywhere you please. Because it prints out it's language to html, this means you can serve up xemark to anything that will pump out html for you.

I just franken coded a rails application to do this.

Now in no way, shape or form, is this good code or practice. But here are the simple steps:


  1. Get a copy of xemark. (You can fork the repository, download it, or whathave you, then just compile parser.c with the -o flag set to xemark
  2. Make yourself a xe file. Heck, use the example.xemark file and rename it example.xe it's up to you.
  3. Next hop to your console and run the commands
    1. rails new railsmark
    2. rails generate controller page showit
  4. Next we need to put in some code to show call xemark so throw this into your page_controller.rb
    class PageController < ApplicationController
      def showit
       p = `./xemark < #{params[:page]}.xe`
    
       respond_to do |format|
        format.html {render :text => p}
       end
      end
    end
    
  5. Finally, move the xemark executable and the example.xe file into the root directory of your rails install.
  6. run rails s or rails server
  7. Navigate to localhost:3000/page/showit?page=example
Tada! You've just served up a xe page through rails. Note that this is a toy example, primarily because if you try to include any style to the xe page they won't link well, and not to mention this doesn't make use of the asset pipeline at all in rails, so no styling for you. Still. It was a fun quick and dirty experiment that turned out surprisingly well.

UPDATE:

I made it serve up my style as well. This is assuming that the xe file links to a stylesheet called style.css (by using ~style in the xe file under the @title)

First off. Change the controller code to have a new function
def style
   p = `cat ./style.css`
   respond_to do |format|
    format.html {render :text => p}
   end
end


Next, add the following to your config/routes.rb file
get "page/style.css", to: "page#style"

And finally, move a file called style.css to the root level of the directory.

And bam. You have style being served.

If I feel like playing more with this, I might make it scan the asset pipeline for the file instead of having it need to be in the root level. For more security, you could do something like this in routes.rb:

get "example", to: "page#showit" , :page => "example"

Once you do that though, you'll have to change your style get to be just style.css instead of page/style.css though.

No comments:

Post a Comment