Best way to serve README.md

70 views
Skip to first unread message

Pavel Serikov

unread,
Mar 28, 2016, 7:50:08 AM3/28/16
to Mojolicious
I want that my README.md will be available under /readme route. How to do that?

I checked the docs about ways of serving static files, but problem that I need to process markdown to html firstly. It's possible to do with Text::Markdown or other plugins, but how to use it together with reply->static helper? 
Or maybe there is ready-to-use plugin? I found Mojolicious::Plugin::Directory, but seems like it serve all static files, but I need to serve only one.

Stefan Adams

unread,
Mar 28, 2016, 10:28:27 AM3/28/16
to mojolicious
TMTOWTDI

$ cat md
#!/usr/bin/env perl
use Mojolicious::Lite;

use Text::Markdown;

helper md => sub { shift; Text::Markdown->new->markdown(@_) };

get '/readme' => sub {
  my $c = shift;
  $c->render(text => $c->md(Mojo::Util::slurp 'README.md'));
};

app->start;

$ cat README.md 
# Hello

$ perl md get /readme
[Mon Mar 28 09:26:17 2016] [debug] GET "/readme"
[Mon Mar 28 09:26:17 2016] [debug] Routing to a callback
[Mon Mar 28 09:26:17 2016] [debug] 200 OK (0.002154s, 464.253/s)
<h1>Hello</h1>


--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Brian Duggan

unread,
Mar 28, 2016, 12:43:48 PM3/28/16
to mojol...@googlegroups.com
On Monday, March 28, Stefan Adams wrote:
> TMTOWTDI

Another way would be with a handler:

app->renderer->add_handler( 'md' => sub {
my ($r, $c, $out, $opts) = @_;
$$out = Text::Markdown->new->markdown(
Mojo::Util::slurp($r->template_path($opts)))
} );

get 'readme';

(though this would look for templates/readme.html.md)

Brian
Reply all
Reply to author
Forward
0 new messages