Last active
November 16, 2019 14:41
-
-
Save 5hanth/1486ecf583845812f319 to your computer and use it in GitHub Desktop.
Parse text of links to json in elisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"links": [ | |
{ | |
"href": "http:\/\/github.com", | |
"label": "Github" | |
}, | |
{ | |
"href": "http:\/\/stackoverflow.com", | |
"label": "Stackoverflow" | |
}, | |
{ | |
"href": "http:\/\/twitter.com", | |
"label": "Twitter" | |
}, | |
{ | |
"href": "http:\/\/google.com", | |
"label": "Google" | |
} | |
] | |
}# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; emacs -batch -l text-to-json.el --eval='(txt-to-json "out.json" "to-parse.txt")' | |
(require 'cl) | |
(require 'json) | |
(defun txt-to-json (&optional out-file src-file) | |
(let* ((current-buf (buffer-file-name)) | |
(parsed (with-temp-buffer | |
(insert-file-contents (or src-file current-buf)) | |
(split-string | |
(buffer-substring-no-properties | |
(point-min) (point-max)) | |
"\n")))) | |
(with-temp-buffer | |
(insert | |
(json-encode `(:links | |
,(loop for label in parsed | |
by #'cddr | |
for href in (cdr parsed) | |
by #'cddr | |
collect (list (cons :label label) | |
(cons :href href)))))) | |
(json-pretty-print (point-min) | |
(point-max)) | |
(if out-file | |
(write-region nil nil out-file) | |
(buffer-string))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Github | |
http://github.com | |
Stackoverflow | |
http://stackoverflow.com | |
http://twitter.com | |
http://google.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment