Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Sambal] extracted/removed Markdown

It's now its own module.

We're keeping all the Markdown-specific tests for now, though.
Good with a bit of redundancy in the tests.
  • Loading branch information...
commit 759c988308de07fc89d47b929fcf8d3957c45f1b 1 parent 99ee755
Carl Mäsak authored

Showing 2 changed files with 7 additions and 153 deletions. Show diff stats Hide diff stats

  1. +3 149 lib/Sambal.pm
  2. +4 4 t/text.t
152 lib/Sambal.pm
... ... @@ -1,153 +1,6 @@
1 1 module Sambal;
2 2
3   -class Text {
4   - has $.text;
5   - has @.children;
6   -}
7   -
8   -class TSpan {
9   - has $.text;
10   - has $.font-style = '';
11   - has $.font-weight = '';
12   - has $.font-family = '';
13   -}
14   -
15   -my %g_escape_table = <
16   - \\ 28d397e87306b8631f3ed80d858d35f0
17   - ] 0fbd1776e1ad22c59a7080d35c7fd4db
18   - { f95b70fdc3088560732a5ac135644506
19   - # 01abfc750a0c942167651c40d088531d
20   - \> cedf8da05466bb54708268b3c694a78f
21   - * 3389dae361af79b04c9c8e7057f60cc6
22   - _ b14a7b8059d9c055954c92674ce60032
23   - + 26b17225b626fb9238849fd60eabdf60
24   - - 336d5ebc5436534e61d16e63ddfca327
25   - ) 9371d7a2e3ae86a00aab4771e39d255d
26   - . 5058f1af8388633f609cadb75a75dc9d
27   - [ 815417267f76f6f460a4a61f9db75fdb
28   - ( 84c40473414caf2ed4a7b1283e48bbf4
29   - ` 833344d5e1432da82ef02e1301477ce8
30   - } cbb184dd8e05c9709e5dcaedaa0495cf
31   - ! 9033e0e305f247c0c3c80d0c7848c8b3
32   ->;
33   -
34   -sub _EncodeCode($_ is copy) {
35   -#
36   -# Encode/escape certain characters inside Markdown code runs.
37   -# The point is that in code, these characters are literals,
38   -# and lose their special Markdown meanings.
39   -#
40   - # Encode all ampersands; HTML entities are not
41   - # entities within a Markdown code span.
42   - s:g['&'] = '&amp';
43   -
44   - # Do the angle bracket song and dance:
45   - s:g['<'] = '&lt;';
46   - s:g['>'] = '&gt;';
47   -
48   - # Now, escape characters that are magic in Markdown:
49   - s:g[ '*' ] = %g_escape_table<*>;
50   - s:g[ '_' ] = %g_escape_table<_>;
51   - s:g[ '{' ] = %g_escape_table<{>;
52   - s:g[ '}' ] = %g_escape_table<}>;
53   - s:g[ '[' ] = %g_escape_table<[>;
54   - s:g[ ']' ] = %g_escape_table<]>;
55   - s:g[ '\\' ] = %g_escape_table<\\>;
56   -
57   - return $_;
58   -}
59   -
60   -sub _DoCodeSpans($text is copy) {
61   -#
62   -# * Backtick quotes are used for <code></code> spans.
63   -#
64   -# * You can use multiple backticks as the delimiters if you want to
65   -# include literal backticks in the code span. So, this input:
66   -#
67   -# Just type ``foo `bar` baz`` at the prompt.
68   -#
69   -# Will translate to:
70   -#
71   -# <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
72   -#
73   -# There's no arbitrary limit to the number of backticks you
74   -# can use as delimters. If you need three consecutive backticks
75   -# in your code, use four for delimiters, etc.
76   -#
77   -# * You can use spaces to get literal backticks at the edges:
78   -#
79   -# ... type `` `bar` `` ...
80   -#
81   -# Turns to:
82   -#
83   -# ... type <code>`bar`</code> ...
84   -#
85   -
86   - $text ~~ s:g[ ('`'+) (.+?) <!after '`'> $0 <!before '`'> ] = (
87   - my $c = $1.trim;
88   - $c = _EncodeCode($c);
89   - "<code>{$c}</code>"
90   - );
91   -
92   - return $text;
93   -}
94   -
95   -sub _DoItalicsAndBold($text is copy) {
96   - # <strong> must go first:
97   - $text ~~ s:g[ ('**'||'__') <?before \S> (.+?<[*_]>*) <?after \S> $0 ]
98   - = "<strong>{$1}</strong>";
99   -
100   - $text ~~ s:g[ ('*'||'_') <?before \S> (.+?) <?after \S> $0 ]
101   - = "<em>{$1}</em>";
102   -
103   - return $text;
104   -}
105   -
106   -sub _UnescapeSpecialChars($text) {
107   -#
108   -# Swap back in all the special characters we've hidden.
109   -#
110   - return $text.trans( [%g_escape_table.values] => [%g_escape_table.keys] );
111   -}
112   -
113   -sub extract_tspans($text) {
114   - # XXX the below regex wouldn't work for e.g. <b><em><b>foo</b></em></b>
115   - gather for $text.split(/'<'(\w+)'>'.*?'</'$0'>'/, :all) -> $normal, $taggy? {
116   - take TSpan.new(:text($normal));
117   - if $taggy {
118   - # XXX highly specialized but works for our immediate purposes
119   - $taggy.Str ~~ /^ ['<'(\w+)'>']+ (.*?) ['</'\w+'>']+ $/;
120   - my @tags = $0».Str;
121   - my $contents = $1;
122   - my %attrs;
123   - if any(@tags) eq 'em' { %attrs<font-style> = 'italics' }
124   - if any(@tags) eq 'strong' { %attrs<font-weight> = 'bold' }
125   - if any(@tags) eq 'code' { %attrs<font-family> = 'monospace' }
126   - take TSpan.new(:text($contents), |%attrs);
127   - }
128   - }
129   -}
130   -
131   -grammar Markdown {
132   - token TOP {
133   - ^ <paragraph>* % [\n\n+] $
134   - { make $<paragraph>».ast }
135   - }
136   -
137   - token paragraph {
138   - [<!before \n\n> .]+
139   - {
140   - my $text = ~$/;
141   - $text = _DoCodeSpans($text);
142   - $text = _DoItalicsAndBold($text);
143   - $text = _UnescapeSpecialChars($text);
144   -
145   - my @children = extract_tspans($text);
146   -
147   - make Text.new(:$text, :@children);
148   - }
149   - }
150   -}
  3 +use Text::Markdown;
151 4
152 5 class Slide {
153 6 has @.children;
@@ -160,7 +13,8 @@ class Slide {
160 13 my @slide_queue;
161 14
162 15 sub text(Cool $text) is export {
163   - my @paragraphs = Markdown.parse($text).ast.list;
  16 + my $doc = parse-markdown($text);
  17 + my @paragraphs = $doc.children;
164 18 push @slide_queue, Slide.new(@paragraphs);
165 19 }
166 20
8 t/text.t
@@ -29,7 +29,7 @@ use Sambal;
29 29 is +@slides, 1, "Create one text slide";
30 30 is +@slides[0].children, 1, "The slide has one child";
31 31 my $text = @slides[0].children[0];
32   - isa_ok $text, Sambal::Text;
  32 + isa_ok $text, Text::Markdown::Para;
33 33 is +$text.children, 3, "The text has three elements";
34 34 is $text.children[0].text, "One slide with ", 'correct 1/3 tspan';
35 35 is $text.children[1].text, "italics", 'correct 2/3 tspan';
@@ -47,7 +47,7 @@ use Sambal;
47 47 is +@slides, 1, "Create one text slide";
48 48 is +@slides[0].children, 1, "The slide has one child";
49 49 my $text = @slides[0].children[0];
50   - isa_ok $text, Sambal::Text;
  50 + isa_ok $text, Text::Markdown::Para;
51 51 is +$text.children, 3, "The text has three elements";
52 52 is $text.children[0].text, "One slide with ", 'correct 1/3 tspan';
53 53 is $text.children[1].text, "bold", 'correct 2/3 tspan';
@@ -74,7 +74,7 @@ use Sambal;
74 74 is +@slides, 1, "Create one text slide";
75 75 is +@slides[0].children, 1, "The slide has one child";
76 76 my $text = @slides[0].children[0];
77   - isa_ok $text, Sambal::Text;
  77 + isa_ok $text, Text::Markdown::Para;
78 78 is +$text.children, 7, "The text has seven elements";
79 79 is $text.children[0].text, "Now we use both ", 'correct 1/7 tspan';
80 80 is $text.children[1].text, "italics", 'correct 2/7 tspan';
@@ -106,7 +106,7 @@ use Sambal;
106 106 is +@slides, 1, "Create one text slide";
107 107 is +@slides[0].children, 1, "The slide has one child";
108 108 my $text = @slides[0].children[0];
109   - isa_ok $text, Sambal::Text;
  109 + isa_ok $text, Text::Markdown::Para;
110 110 is +$text.children, 3, "The text has 3 elements.";
111 111 is $text.children[0].text, "This text contains ", 'correct 1/3 tspan';
112 112 is $text.children[1].text, "code", 'correct 2/3 tspan';

0 comments on commit 759c988

Please sign in to comment.
Something went wrong with that request. Please try again.