Render dummy headers

This commit is contained in:
Scott Wang 2016-01-04 00:15:31 +08:00
parent 44514aeea4
commit ea7437a068

View File

@ -88,11 +88,15 @@ holding contextual information."
(format "~~%s~~" contents)) (format "~~%s~~" contents))
;;;; Table-Cell ;;;; Table-Common
(defvar width-cookies nil) (defvar width-cookies nil)
(defvar width-cookies-table nil) (defvar width-cookies-table nil)
(defconst gfm-table-left-border "| ")
(defconst gfm-table-right-border " |")
(defconst gfm-table-separator " | ")
(defun org-gfm-table-col-width (table column info) (defun org-gfm-table-col-width (table column info)
"Return width of TABLE at given COLUMN. INFO is a plist used as "Return width of TABLE at given COLUMN. INFO is a plist used as
communication channel. Width of a column is determined either by communication channel. Width of a column is determined either by
@ -118,8 +122,8 @@ the column."
(max (length (max (length
(org-export-data (org-export-data
(org-element-contents (org-element-contents
(elt ;(if specialp (car (org-element-contents row)) (elt (if specialp (car (org-element-contents row))
(org-element-contents row);) (org-element-contents row))
column)) column))
info)) info))
max-width))) max-width)))
@ -127,6 +131,17 @@ the column."
(puthash column max-width width-cookies)))))) (puthash column max-width width-cookies))))))
(defun org-gfm-make-hline-builder (table info char)
"Return a function to build horizontal line in TABLE with given
CHAR. INFO is a plist used as a communication channel."
`(lambda (col)
(let ((max-width (org-gfm-table-col-width table col info)))
(when (< max-width 1)
(setq max-width 1))
(make-string max-width ,char))))
;;;; Table-Cell
(defun org-gfm-table-cell (table-cell contents info) (defun org-gfm-table-cell (table-cell contents info)
"Transcode TABLE-CELL element from Org into GFM. CONTENTS is content "Transcode TABLE-CELL element from Org into GFM. CONTENTS is content
@ -150,24 +165,23 @@ of the cell. INFO is a plist used as a communication channel."
"Transcode TABLE-ROW element from Org into GFM. CONTENTS is cell "Transcode TABLE-ROW element from Org into GFM. CONTENTS is cell
contents of TABLE-ROW. INFO is a plist used as a communication contents of TABLE-ROW. INFO is a plist used as a communication
channel." channel."
(when (eq 'rule (org-element-property :type table-row)) (let ((table (org-export-get-parent-table table-row)))
(when (and (eq 'rule (org-element-property :type table-row))
;; In GFM, rule is valid only at second row.
(eq 1 (cl-position
table-row
(org-element-map table 'table-row 'identity info))))
(let* ((table (org-export-get-parent-table table-row)) (let* ((table (org-export-get-parent-table table-row))
(left-border "| ") (header-p (org-export-table-row-starts-header-p table-row info))
(vertical " | ") (build-rule (org-gfm-make-hline-builder table info ?-))
(right-border " |")
(build-rule
(function
(lambda (col)
(let ((max-width (org-gfm-table-col-width table col info)))
(make-string max-width ?-)))))
(cols (cdr (org-export-table-dimensions table info)))) (cols (cdr (org-export-table-dimensions table info))))
(setq contents (setq contents
(concat left-border (concat gfm-table-left-border
(mapconcat (lambda (col) (funcall build-rule col)) (mapconcat (lambda (col) (funcall build-rule col))
(number-sequence 0 (- cols 1)) (number-sequence 0 (- cols 1))
vertical) gfm-table-separator)
right-border)))) gfm-table-right-border))))
contents) contents))
@ -177,7 +191,27 @@ channel."
"Transcode TABLE element into Github Flavored Markdown table. "Transcode TABLE element into Github Flavored Markdown table.
CONTENTS is the contents of the table. INFO is a plist holding CONTENTS is the contents of the table. INFO is a plist holding
contextual information." contextual information."
(replace-regexp-in-string "\n\n" "\n" contents)) (let* ((rows (org-element-map table 'table-row 'identity info))
(no-header (or (<= (length rows) 1)
(not (eq 'rule (org-element-property :type (cadr rows))))))
(cols (cdr (org-export-table-dimensions table info)))
(build-dummy-header
(function
(lambda ()
(let ((build-empty-cell (org-gfm-make-hline-builder table info ?\s))
(build-rule (org-gfm-make-hline-builder table info ?-))
(columns (number-sequence 0 (- cols 1))))
(concat gfm-table-left-border
(mapconcat (lambda (col) (funcall build-empty-cell col))
columns
gfm-table-separator)
gfm-table-right-border "\n" gfm-table-left-border
(mapconcat (lambda (col) (funcall build-rule col))
columns
gfm-table-separator)
gfm-table-right-border "\n"))))))
(concat (when no-header (funcall build-dummy-header))
(replace-regexp-in-string "\n\n" "\n" contents))))
;;;; Table of contents ;;;; Table of contents