This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
MarkdownReference.pm
79 lines (69 loc) · 1.9 KB
/
MarkdownReference.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package DDG::Goodie::MarkdownReference;
# ABSTRACT: Provide a cheatsheet for common Markdown syntax
use strict;
use DDG::Goodie;
use JSON;
zci answer_type => 'markdown_reference';
zci is_cached => 1;
triggers startend => (
'markdown', 'md',
);
my $json = share('synonyms.json')->slurp();
my $synonyms = decode_json($json);
sub get_element_from_alias {
my $name = shift;
my %synonyms = %{$synonyms};
while (my ($elt_type, $syns) = each %synonyms) {
return $elt_type if $name eq $elt_type;
return $elt_type if defined $syns->{$name};
};
}
sub get_element_type {
my $query = shift;
return get_element_from_alias $query;
}
sub element_to_subtitle {
my $element = shift;
return ucfirst $element;
}
sub get_results {
my $query = shift;
my $element_type = get_element_type $query or return;
my %elements = map { $_ => 0 } (
'header',
'list',
'emphasis',
'bold',
'blockquote',
'image',
'link',
);
$elements{$element_type} = 1;
my $subtitle = element_to_subtitle $element_type;
return ($subtitle, \%elements);
}
handle remainder => sub {
my $query = shift;
return unless $query;
my ($subtitle, $elements) = get_results $query or return;
return $subtitle,
structured_answer => {
data => {
elements => $elements,
element_type => $subtitle,
title => "Markdown Reference",
},
meta => {
sourceName => 'Daring Fireball',
sourceUrl => 'https://daringfireball.net/projects/markdown/syntax',
},
templates => {
group => 'text',
options => {
subtitle_content => 'DDH.markdown_reference.content',
moreAt => 1,
},
},
};
};
1;