5 min

Testing fenced code block commands

I'm typing this page in Markdown. I'll add another fence code block option, and I'll make it available, regardless of what markup language that I'm using, similar to my other custom formatting commands.

My custom fenced code block command code. / code.. is available when I'm typing in Textile or Markdown/Multimarkdown. The feature limits the window size that displays the code. So if it's a large blob of code, a small read-only text window with vertical and horizontal scrollbars displays the code.

A fence-code block like what's used by the Github Flavored Markdown or specified at CommonMark would display the entire code blob on the web page.

But instead of indenting the entire code blob four spaces on each line like with Markdown, the fenced block only needs to be surrounded by three tildes or three backticks.

I also support a command called code=yes but this turns the entire web page into a code page. Sometimes, that's fine, but most of the time, code text is mixed with normal text on the same web page.

Using my code. / code.. command to display my Perl module that uses the Yo API.

package Yo;

use strict;
use warnings;
use LWP::UserAgent;

my %URLS = (
    'all'         => 'http://api.justyo.co/yoall/',
    'user'        => 'http://api.justyo.co/yo/',
    'subscribers' => 'http://api.justyo.co/subscribers_count/'
);

sub new {
    my ($class, $api_token) = @_;
    my $self = ();
    $self->{api_token} = $api_token;
    $self->{link}      = undef;
    bless($self, $class);                 
    return $self;
}

sub set_link {
    my ($self, $link) = @_;
    $self->{link} = $link;
}

sub all {
    my ($self) = @_;
    my $ua       = LWP::UserAgent->new();
    my $response;
    if ( defined($self->{link}) ) { 
        $response = $ua->post( $URLS{'all'}, { 'api_token' => $self->{api_token}, 'link' => $self->{link} } );
    } else {
        $response = $ua->post( $URLS{'all'}, { 'api_token' => $self->{api_token} } );
    }
    my $rc       = $response->code;
    return $rc;       
}

sub user {
    my ($self, $username) = @_;
    my $ua       = LWP::UserAgent->new();
    my $response;
    if ( defined($self->{link}) ) { 
        $response = $ua->post( $URLS{'user'}, { 'api_token' => $self->{api_token}, 'username' => $username, 'link' => $self->{link} } );
    } else {
        $response = $ua->post( $URLS{'user'}, { 'api_token' => $self->{api_token}, 'username' => $username } );
    }
    my $rc       = $response->code;
    return $rc;       
}

sub subscribers {
    my ($self) = @_;
    my $ua  = LWP::UserAgent->new();
    my $url = $URLS{'subscribers'} . "?api_token=" . $self->{api_token};
    my $response = $ua->get($url);
    my $content  = $response->decoded_content();
    return $content; # return json. example {"result": 123}. let client decode json.
}

sub DESTROY {
    my ($self) = @_;
    $self->{api_token} = undef;
    $self->{link} = undef;
}

1;

With Markdown, I could indent the code four spaces on each line, but that's time consuming for a decently-sized block of code.

Here's a snippet of the above code with each line indented four spaces:

use strict;
use warnings;
use LWP::UserAgent;

my %URLS = (
    'all'         => 'http://api.justyo.co/yoall/',
    'user'        => 'http://api.justyo.co/yo/',
    'subscribers' => 'http://api.justyo.co/subscribers_count/'
);

sub new {
    my ($class, $api_token) = @_;
    my $self = ();
    $self->{api_token} = $api_token;
    $self->{link}      = undef;
    bless($self, $class);                 
    return $self;
}

Now trying a fenced code block by surrounding the code text with three tildes at the start of the code block and three tildes at the end of the code block. For now, I'm using my new fence. / fence.. command that works like my code command except fence will display all the text within the page and not in a limited textarea box.

Example:

fence.sub new {  
    my ($class, $api_token) = @_;  
    my $self = ();  
    $self->{api_token} = $api_token;  
    $self->{link}      = undef;  
    bless($self, $class);           
    return $self;  
}  
 fence..

(Except the fence.. starts at the beginning of the line.)

Produces:

sub new {
    my ($class, $api_token) = @_;
    my $self = ();
    $self->{api_token} = $api_token;
    $self->{link}      = undef;
    bless($self, $class);                 
    return $self;
}

I don't understand why CommonMark's spec initiative supports two ways to define a fenced code block. One positive for Textile is that it limits the ways to create headings, bullet points, etc.

Markdown allows multiple ways to bold text while Textile only permits one way. The latter makes for more consistent markup. If using Markdown, I would think that someone would need to create a style manual. For example, use only asterisks to indicate bullet points instead of asterisks or the hyphen.

For Markdown, I only use the pound sign to indicate headings. I don't use a series of hyphens to underline heading text. That seems strange to me.

Here are my custom formatting commands for block and single-line text with some dating back to my Parula code that I created in 2005, which powers ToledoTalk.

  • q. and q..
  • tmpl. and tmpl..
  • hr.
  • br.
  • more.
  • code. and code..
  • fence. and fence..

Another thing that I like about Textile is how it uses alpha characters to define some commands, which makes more sense than punctuation marks. Textile commands use both punctuation and alpha-numeric commands, while Markdown uses only or mainly punctuation marks.

It seems that h2. is more intuitive than two pound signs. A bq. for a blockquote also makes more sense than a greater-than symbol. And so on.

Maybe that's the difference between the two markup languages. Markdown was created for geeks, and Textile was created for normal people.

Obivously over the years, I created my custom formatting commands to read more like Textile commands with the periods.

For block text, my opening and closing commands that use a single period and a double period respectively are easy to parse and substitute, regex-wise within my code.

And the words "code" and "fence" make more sense to me than three backticks or three tildes, although I have added confusion by creating two commands that do similar things. But now I like both commands for their display differences within a web page.

I guess that I'll continue to create my own non-standard path of creating a markup language that works for me.

I like aspects of Textile, and I like aspects of Markdown, which is why I support both markups in my Junco and Grebe codebases. I also support Multimarkdown in both too. For Grebe, it's either Textile or Multimarkdown. For Junco, I can specify whether to use only Markdown or Multimarkdown.

And for my Grebe code, I've added to the Markdown module some Textile formatting commands that I've used for years. So my Grebe code contains my own flavored Markdown.

#junco - #testing - #markup - #markdown - #textile

From JR's : articles
1053 words - 6929 chars - 5 min read
created on
updated on - #
source - versions

Related articles
Test post using Markdown - Mar 10, 2014
Markdown Wars - September 2014 - Sep 08, 2014
Markup Languages - Textile and Markdown - May 15, 2014
ToledoTalk.com New Help Page - Sep 07, 2014
Resolved - Add Markdown support - Jul 02, 2013
more >>



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: Nov 14, 2024 - 9:20 p.m. EST