aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2019-09-30 07:17:12 +0200
committerLars Ingebrigtsen2019-09-30 07:17:12 +0200
commit9f9dca57c60033e6f3248f1492178fed57f3b552 (patch)
treeb5939d02e40293c4eae5028042b85453fc086dc4
parent150bf03107791cd413fa635665401a808b015b4f (diff)
downloademacs-9f9dca57c60033e6f3248f1492178fed57f3b552.tar.gz
emacs-9f9dca57c60033e6f3248f1492178fed57f3b552.zip
shr table fix refactoring
* lisp/net/shr.el (shr--fix-table): Refactor out into own function from... (shr-tag-table): ... this function.
-rw-r--r--lisp/net/shr.el170
1 files changed, 86 insertions, 84 deletions
diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 0cd15dcfe07..64263903bdf 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -1993,19 +1993,75 @@ BASE is the URL of the HTML being rendered."
1993 `(tbody nil ,@(cl-reduce 'append 1993 `(tbody nil ,@(cl-reduce 'append
1994 (mapcar 'dom-non-text-children tbodies))))))) 1994 (mapcar 'dom-non-text-children tbodies)))))))
1995 1995
1996(defun shr--fix-table (dom caption header footer)
1997 (let* ((body (dom-non-text-children (shr-table-body dom)))
1998 (nheader (if header (shr-max-columns header)))
1999 (nbody (if body (shr-max-columns body) 0))
2000 (nfooter (if footer (shr-max-columns footer))))
2001 (nconc
2002 (list 'table nil)
2003 (if caption `((tr nil (td nil ,@caption))))
2004 (cond
2005 (header
2006 (if footer
2007 ;; header + body + footer
2008 (if (= nheader nbody)
2009 (if (= nbody nfooter)
2010 `((tr nil (td nil (table nil
2011 (tbody nil ,@header
2012 ,@body ,@footer)))))
2013 (nconc `((tr nil (td nil (table nil
2014 (tbody nil ,@header
2015 ,@body)))))
2016 (if (= nfooter 1)
2017 footer
2018 `((tr nil (td nil (table
2019 nil (tbody
2020 nil ,@footer))))))))
2021 (nconc `((tr nil (td nil (table nil (tbody
2022 nil ,@header)))))
2023 (if (= nbody nfooter)
2024 `((tr nil (td nil (table
2025 nil (tbody nil ,@body
2026 ,@footer)))))
2027 (nconc `((tr nil (td nil (table
2028 nil (tbody nil
2029 ,@body)))))
2030 (if (= nfooter 1)
2031 footer
2032 `((tr nil (td nil (table
2033 nil
2034 (tbody
2035 nil
2036 ,@footer))))))))))
2037 ;; header + body
2038 (if (= nheader nbody)
2039 `((tr nil (td nil (table nil (tbody nil ,@header
2040 ,@body)))))
2041 (if (= nheader 1)
2042 `(,@header (tr nil (td nil (table
2043 nil (tbody nil ,@body)))))
2044 `((tr nil (td nil (table nil (tbody nil ,@header))))
2045 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
2046 (footer
2047 ;; body + footer
2048 (if (= nbody nfooter)
2049 `((tr nil (td nil (table
2050 nil (tbody nil ,@body ,@footer)))))
2051 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
2052 (if (= nfooter 1)
2053 footer
2054 `((tr nil (td nil (table
2055 nil (tbody nil ,@footer)))))))))
2056 (caption
2057 `((tr nil (td nil (table nil (tbody nil ,@body))))))
2058 (body)))))
2059
1996(defun shr-tag-table (dom) 2060(defun shr-tag-table (dom)
1997 (shr-ensure-paragraph) 2061 (shr-ensure-paragraph)
1998 (let* ((caption (dom-children (dom-child-by-tag dom 'caption))) 2062 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1999 (header (dom-non-text-children (dom-child-by-tag dom 'thead))) 2063 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
2000 (body (dom-non-text-children (shr-table-body dom))) 2064 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot))))
2001 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
2002 (bgcolor (dom-attr dom 'bgcolor))
2003 (start (point))
2004 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
2005 shr-stylesheet))
2006 (nheader (if header (shr-max-columns header)))
2007 (nbody (if body (shr-max-columns body) 0))
2008 (nfooter (if footer (shr-max-columns footer))))
2009 (if (and (not caption) 2065 (if (and (not caption)
2010 (not header) 2066 (not header)
2011 (not (dom-child-by-tag dom 'tbody)) 2067 (not (dom-child-by-tag dom 'tbody))
@@ -2018,83 +2074,29 @@ BASE is the URL of the HTML being rendered."
2018 (if (dom-attr dom 'shr-fixed-table) 2074 (if (dom-attr dom 'shr-fixed-table)
2019 (shr-tag-table-1 dom) 2075 (shr-tag-table-1 dom)
2020 ;; Only fix up the table once. 2076 ;; Only fix up the table once.
2021 (let ((table 2077 (let ((table (shr--fix-table dom caption header footer)))
2022 (nconc
2023 (list 'table nil)
2024 (if caption `((tr nil (td nil ,@caption))))
2025 (cond
2026 (header
2027 (if footer
2028 ;; header + body + footer
2029 (if (= nheader nbody)
2030 (if (= nbody nfooter)
2031 `((tr nil (td nil (table nil
2032 (tbody nil ,@header
2033 ,@body ,@footer)))))
2034 (nconc `((tr nil (td nil (table nil
2035 (tbody nil ,@header
2036 ,@body)))))
2037 (if (= nfooter 1)
2038 footer
2039 `((tr nil (td nil (table
2040 nil (tbody
2041 nil ,@footer))))))))
2042 (nconc `((tr nil (td nil (table nil (tbody
2043 nil ,@header)))))
2044 (if (= nbody nfooter)
2045 `((tr nil (td nil (table
2046 nil (tbody nil ,@body
2047 ,@footer)))))
2048 (nconc `((tr nil (td nil (table
2049 nil (tbody nil
2050 ,@body)))))
2051 (if (= nfooter 1)
2052 footer
2053 `((tr nil (td nil (table
2054 nil
2055 (tbody
2056 nil
2057 ,@footer))))))))))
2058 ;; header + body
2059 (if (= nheader nbody)
2060 `((tr nil (td nil (table nil (tbody nil ,@header
2061 ,@body)))))
2062 (if (= nheader 1)
2063 `(,@header (tr nil (td nil (table
2064 nil (tbody nil ,@body)))))
2065 `((tr nil (td nil (table nil (tbody nil ,@header))))
2066 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
2067 (footer
2068 ;; body + footer
2069 (if (= nbody nfooter)
2070 `((tr nil (td nil (table
2071 nil (tbody nil ,@body ,@footer)))))
2072 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
2073 (if (= nfooter 1)
2074 footer
2075 `((tr nil (td nil (table
2076 nil (tbody nil ,@footer)))))))))
2077 (caption
2078 `((tr nil (td nil (table nil (tbody nil ,@body))))))
2079 (body)))))
2080 (dom-set-attribute table 'shr-fixed-table t) 2078 (dom-set-attribute table 'shr-fixed-table t)
2081 (setcdr dom (cdr table)) 2079 (setcdr dom (cdr table))
2082 (shr-tag-table-1 dom)))) 2080 (shr-tag-table-1 dom)))
2083 (when bgcolor 2081 (let* ((bgcolor (dom-attr dom 'bgcolor))
2084 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet)) 2082 (start (point))
2085 bgcolor)) 2083 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
2086 ;; Finally, insert all the images after the table. The Emacs buffer 2084 shr-stylesheet)))
2087 ;; model isn't strong enough to allow us to put the images actually 2085 (when bgcolor
2088 ;; into the tables. It inserts also non-td/th objects. 2086 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
2089 (when (zerop shr-table-depth) 2087 bgcolor))
2090 (save-excursion 2088 ;; Finally, insert all the images after the table. The Emacs buffer
2091 (shr-expand-alignments start (point))) 2089 ;; model isn't strong enough to allow us to put the images actually
2092 (let ((strings (shr-collect-extra-strings-in-table dom))) 2090 ;; into the tables. It inserts also non-td/th objects.
2093 (when strings 2091 (when (zerop shr-table-depth)
2094 (save-restriction 2092 (save-excursion
2095 (narrow-to-region (point) (point)) 2093 (shr-expand-alignments start (point)))
2096 (insert (mapconcat #'identity strings "\n")) 2094 (let ((strings (shr-collect-extra-strings-in-table dom)))
2097 (shr-fill-lines (point-min) (point-max)))))))) 2095 (when strings
2096 (save-restriction
2097 (narrow-to-region (point) (point))
2098 (insert (mapconcat #'identity strings "\n"))
2099 (shr-fill-lines (point-min) (point-max))))))))))
2098 2100
2099(defun shr-collect-extra-strings-in-table (dom &optional flags) 2101(defun shr-collect-extra-strings-in-table (dom &optional flags)
2100 "Return extra strings in DOM of which the root is a table clause. 2102 "Return extra strings in DOM of which the root is a table clause.