Skip to content

Commit

Permalink
support table dividers
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfenner committed Feb 8, 2024
1 parent 5a0e11e commit 22efcb0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
66 changes: 4 additions & 62 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -388,65 +388,10 @@ TEMP_PDF_FILE="$(basename ${input_file}).pdf"

LATEX_LOG="${build_dir}/latex.log"

# Get the time in Unix epoch milliseconds of the given line
timestamp_of() {
local LINE="$1"
local TIMESTAMP=$(echo "${LINE}" | cut -d ' ' -f 1 | tr -d "[]")


local SECONDS=$(echo "${TIMESTAMP}" | cut -d '.' -f 1)
local MILLISECONDS=$(echo "${TIMESTAMP}" | cut -d '.' -f 2 | head -c 3)
# MILLISECONDS might have some leading 0's. Trim them by converting it as a base-10 integer.
echo $(( $SECONDS * 1000 + 10#$MILLISECONDS ))
}

# Get the duration in human-readable time between two patterns in the logfile
time_between() {
local LOGFILE="$1"
local FIRST_PATTERN="$2"
local SECOND_PATTERN="$3"

local FIRST_LINE=$(grep "${FIRST_PATTERN}" "${LOGFILE}" | head -n 1)
local SECOND_LINE=$(grep "${SECOND_PATTERN}" "${LOGFILE}" | head -n 1)

if [ -z "${FIRST_LINE}" -o -z "${SECOND_LINE}" ]; then
echo "n/a"
else
local FIRST_TIME=$(timestamp_of "${FIRST_LINE}")
local SECOND_TIME=$(timestamp_of "${SECOND_LINE}")

ELAPSED_MS=$(( ${SECOND_TIME} - ${FIRST_TIME} ))

ELAPSED_S=$(( $ELAPSED_MS / 1000 ))
ELAPSED_MS=$(( $ELAPSED_MS % 1000 ))

ELAPSED_M=$(( $ELAPSED_S / 60 ))
ELAPSED_S=$(( $ELAPSED_S % 60 ))

ELAPSED_MS="${ELAPSED_MS}ms"

if [ ${ELAPSED_M} -gt 0 ]; then
ELAPSED_M="${ELAPSED_M}m "
# Don't print the milliseconds if we got more than a minute.
ELAPSED_MS=""
else
ELAPSED_M=""
fi

if [ ${ELAPSED_S} -gt 0 ]; then
ELAPSED_S="${ELAPSED_S}s "
else
ELAPSED_S=""
fi

echo "${ELAPSED_M}${ELAPSED_S}${ELAPSED_MS}"
fi
}

analyze_latex_logs() {
local LOGFILE=$1

local RUNCOUNT=$(grep "Run number " "${LOGFILE}" | tail -n 1 | cut -d ' ' -f 4)
local RUNCOUNT=$(grep "Run number " "${LOGFILE}" | tail -n 1 | cut -d ' ' -f 3)
local PASSES="passes"
if [ "${RUNCOUNT}" -eq "1" ]; then
PASSES="pass"
Expand All @@ -457,10 +402,7 @@ analyze_latex_logs() {
local WARNINGS=$(sed -n "/Run number ${RUNCOUNT}/,$ p" "${LOGFILE}" | grep "LaTeX Warning: ")
if [ ! -z "${WARNINGS}" ]; then
echo "LaTeX warnings (may be ignorable - check the output!):"
echo "${WARNINGS}" | while read LINE; do
local CUT_LINE=$(echo "${LINE}" | cut -d ' ' -f2-)
echo " ${CUT_LINE}"
done
echo "${WARNINGS}"
fi
}

Expand Down Expand Up @@ -521,7 +463,7 @@ if [ -n "${pdf_output}" -o -n "${latex_output}" ]; then
start=$(date +%s)
# Run twice to populate aux, lof, lot, toc, then update the page numbers due
# to the impact of populating the lof, lot, toc.
latexmk "${TEMP_TEX_FILE}" -pdflatex=xelatex -pdf -diagnostics | ts '[%.s]' > "${LATEX_LOG}"
latexmk "${TEMP_TEX_FILE}" -pdflatex=xelatex -pdf -diagnostics > "${LATEX_LOG}"
if [ $? -ne 0 ]; then
FAILED=true
echo "PDF output failed"
Expand All @@ -538,7 +480,7 @@ if [ -n "${pdf_output}" -o -n "${latex_output}" ]; then
fi
echo "Elapsed time: $(($end-$start)) seconds"
# Write any LaTeX errors to stderr.
>&2 grep -A 5 "] ! " "${LATEX_LOG}"
>&2 grep -A 5 "! " "${LATEX_LOG}"
if [[ ! "${FAILED}" = "true" ]]; then
mv "${TEMP_PDF_FILE}" "${pdf_output}"
analyze_latex_logs "${LATEX_LOG}"
Expand Down
24 changes: 24 additions & 0 deletions filter/tabularx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,33 @@ function GetCellCode(cell)
return cell_code
end

-- For === Special Header === rows in rowspans.
function SpecialSeparatorRow(width, contents, i, plain)
local code = ''
local colspec = 'c'
if not plain then
code = code .. '\\hline'
colspec = '|c|'
end

local inner_contents = string.format('\\textbf{\\textit{\\footnotesize %s}}', contents)
code = code .. string.format('\\multicolumn{%d}{%s}{\\cellcolor{table-section-background}\\textcolor{table-section-foreground}{%s}} \\\\*\n', width, colspec, inner_contents)

return code
end

-- TODO: This code's a real spaghetti factory. Refactor it in the future.
function TabularRow(height, colspecs, skips, rows_with_rowspans, row, i, plain, no_first_hline, header)
local width = Length(colspecs)

-- Special case: this row's first cell spans the entire table and begins and ends with ===.
if row.cells[1] and row.cells[1].col_span == width then
local contents = pandoc.utils.stringify(row.cells[1].contents):match('===+ ?(.*) ===+')
if contents then
return SpecialSeparatorRow(width, contents, i, plain)
end
end

local n = 1
-- Prepare a list of latex snippets to be concatenated together below.
local row_code = {}
Expand Down
54 changes: 54 additions & 0 deletions guide.tcg
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,60 @@ Table: Fruits (Grid) {#tbl:fruits-grid}
| Banana | Yellow | No |
+-------------+--------+----------------------------+

A grid table can be divided into sections using `===` in a full-span row,
as in @tbl:some-command:

```md
Table: Some Command {#tbl:some-command}

+-------------+--------+----------------------------+
| Type | Name | Description |
+=============+========+============================+
| ================= Header ======================== |
+-------------+--------+----------------------------+
| UINT32 | Siz | The size of the following |
| | | contents. |
+-------------+--------+----------------------------+
| ================ Contents ======================= |
+-------------+--------+----------------------------+
| BYTE\[Siz\] | Items | `Size` many items. |
+-------------+--------+----------------------------+
```

Table: Some Command {#tbl:some-command}

+-------------+--------+----------------------------+
| Type | Name | Description |
+=============+========+============================+
| ================= Header ======================== |
+-------------+--------+----------------------------+
| UINT32 | Siz | The size of the following |
| | | contents. |
+-------------+--------+----------------------------+
| ================ Contents ======================= |
+-------------+--------+----------------------------+
| BYTE\[Siz\] | Items | `Siz` many items. |
+-------------+--------+----------------------------+

* The row must span the entire table
* The row must begin and end with at least three equals signs
* The row must be separated from its equals signs by spaces

Below is a table with full-span rows that do not use this feature:

+-------------+--------+----------------------------+
| Type | Name | Description |
+=============+========+============================+
| Header |
+-------------+--------+----------------------------+
| UINT32 | Siz | The size of the following |
| | | contents. |
+-------------+--------+----------------------------+
| Contents |
+-------------+--------+----------------------------+
| BYTE\[Siz\] | Items | `Siz` many items. |
+-------------+--------+----------------------------+

### Styling Markdown Tables

#### Removing Lines
Expand Down
2 changes: 2 additions & 0 deletions template/tcg.tex
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@
\definecolor{informative-header}{RGB}{75, 77, 78}
\definecolor{informative-foreground}{RGB}{10, 10, 10}

\definecolor{table-section-background}{RGB}{224, 224, 224}
\definecolor{table-section-foreground}{RGB}{75, 77, 78}

%
% code block style
Expand Down

0 comments on commit 22efcb0

Please sign in to comment.