all files / lib/standard/ markdown.js

100% Statements 35/35
100% Branches 2/2
100% Functions 18/18
100% Lines 35/35
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                  10×                 26×                           15× 12×     14×                       12×              
/*
 * Parsec
 * https://github.com/d-plaindoux/parsec
 *
 * Copyright (c) 2016 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */
 
 module.exports = (function() {
     
     'use strict';
     
     var P = require('../parsec/parser.js');        
     
     //
     // Facilities
     // 
     
     var eol = P.char('\n');
     
     function title(line, level) {
         return { title : line, level : level };
     }
     
     function italicText(text) {
         return { italic : text };
     }
 
     function boldText(text) {
         return { bold : text };
     }
 
     function strikeText(text) {
         return { strike : text };
     }
 
     function normalText(c) {
         return { text : c.join('') };
     }
 
     function italic() {
         return P.try(P.string("*").thenRight(P.lazy(text,["*"])).thenLeft(P.string("*")).
                or(P.string("_").thenRight(P.lazy(text,["_"])).thenLeft(P.string("_"))).
                map(italicText));
     }
 
     function bold() {
         return P.try(P.string("**").thenRight(P.lazy(text,["**"])).thenLeft(P.string("**")).
                or(P.string("__").thenRight(P.lazy(text,["__"])).thenLeft(P.string("__"))).
                map(boldText));
     }
     
     function strike() {
         return P.try(P.string("~~").thenRight(P.lazy(text,["~~"])).thenLeft(P.string("~~")).
                map(strikeText));
     }
     
     function text(separator) {
         if (separator) {
             return P.not(eol.or(P.string(separator))).optrep().
                    map(normalText);
         } else {
             return P.not(eol).then(P.charNotIn('\n*_~').optrep()).
                    map(function (c) { return [c[0]].concat(c[1]); }).
                    map(normalText);
         }
     }
     
     function line() {
         return (bold().or(italic()).or(strike()).or(text())).optrep().
                thenLeft(eol.opt());
     }
     
     function titleSharp() {
         return P.char('#').rep().then(line()).map(function(c) {
             return [ title(c[1], c[0].length) ];    
         });
     }
 
     function titleAlt() {
         return P.try(
             line().flatmap(function(l) {
                 return P.char('=').rep().map(function () {
                     return [ title(l,1) ];
                }).or(P.char('-').rep().map(function () {
                     return [ title(l,2) ];
                })).thenLeft(eol.opt());
             })
         );
     }
     
     return titleSharp().or(titleAlt()).or(line()).thenLeft(P.eos);
 }());