Mojito Simply Does MojoMojo

I've been working on a document engine called Mojito. It supports a subset of the MojoMojo formatters. I was thinking it might be cool to see how my MojoMojo documents look like through the eye of Mojito. The idea being that I tap the documents in a MojoMojo store and render them with Mojiito::Page::Render. Further, I put the pieces together by making a Web::Simple application of it.

Core of the Web App

The gist of the Web::Simple application is:

use strictures 1;
package RenderMojoMojo;
use Web::Simple;
use MojoMojo::Schema;
use Mojito::Page::Render;

with ('Mojito::Role::Config');

sub dispatch_request {

    sub (GET + /**) {
        my ($self, $path) = @_;

        my @parts = split '/', $path;
        my $page_name = $parts[-1];
        my $page_struct = $self->build_page_struct($page_name);
        my $output = $self->render->render_page($page_struct);
        [ 200, [ 'Content-type', 'text/html' ], [$output] ];
      },
}

...

RenderMojoMojo->run_if_script;

We use a dispatcher sub to capture the request path and then pick the last part of the path off as the page name. We then pass this page name to the build_page_struct method that will find a page of that name in the MojoMojo DB and construct of HashRef of page data. Once we've built the page structure then we pass it to the rendering engine to turn the page source into HTML.

build_page_structure

The Mojito::Page::Render class requires a certain type of HashRef as input to its render method. We build that structure in this method. The highlights are:

  • Use the MojoMojo ResultSet Page to find the page with the corresponding name.
  • Construct a HashRef from the page (and its related) parts.
sub build_page_struct {
    my ($self, $page_name) = @_;

    my $page = $self->page_rs->search({ name => $page_name })->first;
    return if not $page;
    my $sections = [ { content => $page->content->body, class => 'Implicit' } ];
    my $datetime = $page->content->created;
    my $title = join ' ', map { ucfirst } split '_', $page->name;

    my $page_struct = {
        sections => $sections,
        page_source => $page->content->body,
        created => $datetime->epoch,
        default_format => $self->wiki_format,
        title => $title,
    };
    
    return $page_struct;
}

Limitations

While this setup will find and present a MojoMojo page with a particular name, we are not checking for duplicates. Since MojoMojo has a tree structure, one could have pages with the same name but in different parts of the tree. In order to guarantee a unique page we'd need to look at a MojoMojo page's lineage. This can be done like so:

sub page_lineage {
    my ($self, $page) = @_;
    my $lineage;
    while (my $parent = $page->parent) {
        if ($parent->id == 1) {
            return '/' . $lineage;
        }
        else {
            $lineage = $parent->name . '/' . $lineage;
            $page    = $parent;
        }
    }
    die
"Should not get HERE.  Means we found page that doesn't have the root page (id = 1) as an ancestor";
}

Running it

I'm running the application as follows:

MOJITO_CONFIG=./mojito_does_mojomojo.conf plackup RenderMojoMojo.pm 

using this source and conf file

Leave a comment

About mateu

user-pic I blog about Perl.