2015-01-10 20:45:29 -08:00
package DDG::Goodie::MarkdownCheatSheet ;
# ABSTRACT: Provide a cheatsheet for common Markdown syntax
2015-02-22 12:09:29 -08:00
use strict ;
2015-01-10 20:45:29 -08:00
use DDG::Goodie ;
2015-01-27 06:45:50 -08:00
use HTML::Entities ;
2015-01-10 20:45:29 -08:00
zci answer_type = > "markdown_cheat" ;
zci is_cached = > 1 ;
name "MarkdownCheatSheet" ;
description "Markdown cheat sheet" ;
source "http://daringfireball.net/projects/markdown/syntax" ;
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/MarkdownCheatSheet.pm" ;
category "cheat_sheets" ;
topics "computing" , "geek" , "web_design" ;
2015-01-21 12:48:39 -08:00
primary_example_queries 'markdown help header' , 'markdown cheat sheet h1' , 'markdown syntax list' ;
secondary_example_queries 'markdown quick reference image' , 'markdown guide headers' ;
2015-01-10 20:45:29 -08:00
triggers startend = > (
2015-01-15 11:40:30 -08:00
'markdown' , 'md' ,
'markdown help' , 'md help' ,
'markdown cheat sheet' , 'md cheat sheet' ,
'markdown cheatsheet' , 'md cheatsheet' ,
'markdown syntax' , 'md syntax' ,
'markdown guide' , 'md guide' ,
'markdown quick reference' , 'md quick reference' ,
'markdown reference' , 'md reference' ,
2015-01-10 20:45:29 -08:00
) ;
attribution github = > [ "marianosimone" , "Mariano Simone" ] ;
2015-01-12 15:29:18 -08:00
# Base snippet definitions
2015-01-11 18:36:19 -08:00
my % snippets = (
2015-01-12 15:10:54 -08:00
'header' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( ' <h1> This is an H1 </h1>
<h2> This is an H2 </h2>
<h6> This is an H6 </h6> ' ) ,
2015-01-12 15:10:54 -08:00
'text' = > ' # This is an H1
2015-01-11 18:55:58 -08:00
## This is an H2
2015-01-12 15:10:54 -08:00
###### This is an H6'
} ,
'em' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( '<em>Emphasis</em> or <em>ephasis</em>' ) ,
2015-01-12 15:10:54 -08:00
'text' = > '_emphasis_ or *emphasis*'
} ,
'strong' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( '<strong>Strong</strong> or <strong>strong</strong>' ) ,
2015-01-12 15:10:54 -08:00
'text' = > '**strong** or __strong__'
} ,
'list' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( ' <ul>
<li> First </li>
<li> Second </li>
</ul>
<ol>
<li> First </li>
<li> Second </li>
</ol> ' ) ,
2015-01-12 15:10:54 -08:00
'text' = > ' - First
2015-01-11 18:36:19 -08:00
- Second
2015-01-12 15:10:54 -08:00
1 . First
2015-01-27 06:45:50 -08:00
2 . Second '
2015-01-12 15:10:54 -08:00
} ,
'image' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( '<img src="https://duckduckgo.com/assets/badges/logo_square.64.png"/>' ) ,
2015-01-21 12:51:59 -08:00
'text' = > '![Image Alt](https://duckduckgo.com/assets/badges/logo_square.64.png)'
2015-01-12 15:10:54 -08:00
} ,
'link' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( '<a href="http://www.duckduckgo.com" title="Example Title">This is an example inline link</a>' ) ,
2015-01-12 15:10:54 -08:00
'text' = > '[This is an example inline link](http://www.duckduckgo.com "Example Title")'
} ,
'blockquote' = > {
2015-01-27 06:45:50 -08:00
'html' = > HTML::Entities:: encode_entities ( '<blockquote>This is the first level of quoting.<blockquote>This is nested blockquote.</blockquote>Back to the first level.</blockquote>' ) ,
2015-01-12 15:10:54 -08:00
'text' = > ' > This is the first level of quoting .
2015-01-11 18:36:19 -08:00
>
> > This is nested blockquote .
>
2015-01-12 15:10:54 -08:00
> Back to the first level . '
}
2015-01-11 18:36:19 -08:00
) ;
2015-01-12 15:29:18 -08:00
my % synonyms = (
2015-01-21 12:48:39 -08:00
'header' , [ 'h1' , 'headers' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' , 'heading' ] ,
'em' , [ 'emphasis' , 'emphasize' , 'italic' , 'italics' ] ,
'strong' , [ 'bold' ] ,
'image' , [ 'img' , 'images' , 'insert image' ] ,
'link' , [ 'a' , 'href' , 'links' ] ,
'blockquote' , [ 'quote' , 'quotation' ] ,
'list' , [ 'lists' , 'ordered list' , 'unordered list' , 'ul' , 'ol' , 'bullet' , 'bullets' ]
2015-01-12 15:29:18 -08:00
) ;
2015-01-11 18:36:19 -08:00
2015-01-12 15:29:18 -08:00
# Add more mappings for each snippet
foreach my $ key ( keys ( % synonyms ) ) {
foreach my $ v ( @ { $ synonyms { $ key } } ) {
$ snippets { $ v } = $ snippets { $ key } ;
2015-01-11 18:36:19 -08:00
}
2015-01-12 15:29:18 -08:00
}
2015-01-10 20:45:29 -08:00
2015-01-27 06:45:50 -08:00
my $ more_at = '<a href="http://daringfireball.net/projects/markdown/syntax" class="zci__more-at--info"><img src="http://daringfireball.net/favicon.ico" class="zci__more-at__icon"/>More at Daring Fireball</a>' ;
2015-01-15 11:55:13 -08:00
2015-01-12 15:10:54 -08:00
sub make_html {
my $ element = $ _ [ 0 ] ;
2015-01-27 06:45:50 -08:00
return 'Markdown:<pre>' . $ snippets { $ element } - > { 'text' } . '</pre>HTML:<pre>' . $ snippets { $ element } - > { 'html' } . '</pre>' . $ more_at
2015-01-12 15:10:54 -08:00
} ;
2015-01-10 20:45:29 -08:00
handle remainder = > sub {
2015-01-11 18:36:19 -08:00
return unless $ _ ;
2015-01-12 15:29:18 -08:00
my $ requested = $ snippets { $ _ } ;
2015-01-11 18:36:19 -08:00
return unless $ requested ;
2015-01-10 20:45:29 -08:00
return
heading = > 'Markdown Cheat Sheet' ,
2015-01-15 11:55:13 -08:00
html = > make_html ( $ _ ) ,
2015-01-12 15:29:18 -08:00
answer = > $ snippets { $ _ } - > { 'text' }
2015-01-10 20:45:29 -08:00
} ;
2015-01-11 18:36:19 -08:00
1 ;