From 348f1a987e248bb83d9c4c188333b67a2b3470cc Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 11 Apr 2018 02:15:26 +0200 Subject: Wrap page title and URL in single quotes with escape Ensure that special characters don't cause the YAML to be invalid by wrapping the URL and page title in quotes to force them to be strings. Escape any literal single quotes in the page title by doubling them. Didn't escape single quotes in the URL because I'm assuming any present will be URL encoded. --- w3m-session-backup.el | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/w3m-session-backup.el b/w3m-session-backup.el index ca7192f..47710d5 100644 --- a/w3m-session-backup.el +++ b/w3m-session-backup.el @@ -27,6 +27,15 @@ ;; Format some YAML text to write to the file ;; Configurable dynamic filename based on date-time +(defun yml-escape (str) + "YAML escape single quotes by doubling them." + (replace-regexp-in-string + (regexp-quote "'") + "''" + str + 'fixedcase + 'literal)) + (defun save-backup () "TODO" (with-temp-file "~/tmp-w3m-session.yml" @@ -34,9 +43,9 @@ (string-join (mapcar (lambda (page) - (format "- page_title: %s - url: %s" - (first (last page)) + (format "- page_title: '%s' + url: '%s'" + (yml-escape (first (last page))) (first page))) (my-w3m-session-backup)) "\n")))) -- cgit v1.2.3