Skip to content

Instantly share code, notes, and snippets.

@ryanobjc
Created March 23, 2019 03:14
Show Gist options
  • Save ryanobjc/d6f3b92bf70e766d08ab584636139b67 to your computer and use it in GitHub Desktop.
Save ryanobjc/d6f3b92bf70e766d08ab584636139b67 to your computer and use it in GitHub Desktop.
example of parsing shr tables
(defun shr-table-to-list (str)
"Turns a chunk of text with 'shr-indentation properties (from the
simple html renderer of tables) into a list of the table data cells"
(let ((pos 0)
(nextpos 0)
(results '())
(strlen (length str)))
(while (and nextpos (< pos strlen))
(setq nextpos (next-single-property-change (1+ pos) 'shr-indentation str))
(if nextpos
(progn (push (string-trim (substring-no-properties str pos (1- nextpos))) results)
(setq pos nextpos)
)))
(push (string-trim (substring-no-properties str pos)) results)
(reverse results)))
(setq test-data #("Netflix​, Netflix (Automatic Renewal) Netflix​, Netflix (Automatic Renewal) $7.99 " 0 1 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face) shr-indentation nil) 1 37 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 37 38 (shr-table-indent 2 display (space :align-to (206)) face ((:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 38 39 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face) shr-indentation 0) 39 75 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 75 76 (shr-table-indent 2 display (space :align-to (466)) face ((:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 76 77 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face) shr-indentation 0) 77 81 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 81 82 (face (variable-pitch (:foreground "#7d7d7d") (:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 82 83 (shr-table-indent 2 display (space :align-to (517)) face ((:foreground "#7d7d7d") (:background "#555555") (:background "#555555") mu4e-view-body-face)) 83 84 (face ((:background "#555555") (:background "#555555") mu4e-view-body-face) display (space :align-to (531)) shr-table-indent 4) 84 85 (face ((:background "#555555") (:background "#555555") mu4e-view-body-face) shr-table-indent 7 display (space :align-to (1217))) 85 86 (face ((:background "#555555") (:background "#555555") mu4e-view-body-face) display (space :align-to (1231)) shr-table-indent 8)))
(shr-table-to-list test-data)
;; returns:
;; ("Netflix​, Netflix (Automatic Renewal)" "Netflix​, Netflix (Automatic Renewal)" "$7.99")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment