add backslash escapes

This commit is contained in:
Erik Edrosa 2016-05-18 23:55:07 -04:00
parent 3e83835b7c
commit b59b3adf79
5 changed files with 56 additions and 2 deletions

View File

@ -5,6 +5,7 @@ godir=$(prefix)/share/guile/site/2.0
SOURCES = \
commonmark/utils.scm \
commonmark/common.scm \
commonmark/node.scm \
commonmark/blocks.scm \
commonmark/inlines.scm \

View File

@ -21,12 +21,12 @@
#:use-module (ice-9 regex)
#:use-module (commonmark node)
#:use-module (commonmark utils)
#:use-module (commonmark common)
#:export (parse-blocks))
;; ']' needs to be the first character after an openning '[' to be able
;; to match ']'
(define ascii-punctuation-characters "[]!\"#$%&'()*+,-./:;<=>?@[\\^_`{|}~]")
(define escaped-characters (string-append "\\\\" ascii-punctuation-characters))
(define escaped-characters (string-append "\\\\[" ascii-punctuation-characters "]"))
(define regular-characters "[^\x01-\x19 ()\\\\]")
(define in-parens-no-space (string-append "\\((" regular-characters "|" escaped-characters "|\\\\)*\\)"))
(define link-label (string-append "\\[(([^][]|"

23
commonmark/common.scm Normal file
View File

@ -0,0 +1,23 @@
;; Copyright (C) 2016 Erik Edrosa <erik.edrosa@gmail.com>
;;
;; This file is part of guile-commonmark
;;
;; guile-commonmark is free software: you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public License
;; as published by the Free Software Foundation, either version 3 of
;; the License, or (at your option) any later version.
;;
;; guile-commonmark is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with guile-commonmark. If not, see <http://www.gnu.org/licenses/>.
(define-module (commonmark common)
#:export (ascii-punctuation-characters))
;; ']' needs to be the first character after an openning '[' to be able
;; to match ']'
(define ascii-punctuation-characters "]!\"#$%&'()*+,-./:;<=>?@[\\^_`{|}~")

View File

@ -22,6 +22,7 @@
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-26)
#:use-module (commonmark node)
#:use-module (commonmark common)
#:export (parse-inlines))
(define re-start-ticks (make-regexp "^`+"))
@ -207,6 +208,24 @@
delim-stack
nodes-stack)))))
(define (ascii-punctuation-characters? ch)
(define ascii-punc-set (string->char-set ascii-punctuation-characters))
(char-set-contains? ascii-punc-set ch))
(define (parse-backslash text nodes delim-stack nodes-stack)
(let* ((next-ch-text (text-advance text 1))
(next-ch (text-char next-ch-text)))
(cond ((char=? next-ch #\newline)
(parse-char (text-advance next-ch-text 1)
(cons (make-hardbreak-node) nodes)
delim-stack nodes-stack))
((ascii-punctuation-characters? next-ch)
(parse-char (text-advance next-ch-text 1)
(cons (make-text-node (string next-ch)) nodes)
delim-stack nodes-stack))
(else (parse-char next-ch-text (cons (make-text-node "\\") nodes)
delim-stack nodes-stack)))))
(define (parse-ticks text nodes delim-stack nodes-stack)
(let ((start-ticks (start-ticks? text)))
(let loop ((end-ticks (end-ticks? (text-move text (match:end start-ticks 0)))))
@ -239,6 +258,7 @@
(if (text-end? text)
(pop-remaining-delim nodes delim-stack nodes-stack)
(case (text-char text)
((#\\) (parse-backslash text nodes delim-stack nodes-stack))
((#\`) (parse-ticks text nodes delim-stack nodes-stack))
((#\* #\_) (parse-emphasis text nodes delim-stack nodes-stack))
(else (parse-normal-text text nodes delim-stack nodes-stack)))))

View File

@ -53,6 +53,8 @@
text-node?
make-softbreak-node
softbreak-node?
make-hardbreak-node
hardbreak-node?
make-blank-node
blank-node?
make-code-span-node
@ -81,6 +83,7 @@
;; - 'heading
;; - 'text
;; - 'softbreak
;; - 'hardbreak
;; - 'blank
;; - 'code-span
;; - 'emphasis
@ -251,6 +254,13 @@
(define (softbreak-node? n)
(node-type? n 'softbreak))
;; Hardbreak node
(define (make-hardbreak-node)
(make-node 'hardbreak '((closed . #t))))
(define (hardbreak-node? n)
(node-type? n 'hardbreak))
;; Blank node
(define (make-blank-node)
(make-node 'blank '((closed . #t))))