Skip to content

Commit

Permalink
Update plugins for pandoc-xnos suite version 2.0 breaking changes
Browse files Browse the repository at this point in the history
merges manubot/rootstock#286
closes manubot/rootstock#284

- removes table scroll plugin
- relies on pandoc-tablenos to wrap `<table>` elements in `<div>`s that can be scrolled
- updates CSS such that breaks are forced when printing or when a table isn't given an id
  (and thus isn't wrapped with a `<div>`
- simplifies plugins a bit as a result of pandoc-tablenos and pandoc-fignos changes
- adds tolerance to scroll-out-of-bounds feature
- adds support for equations in anchors plugin
  • Loading branch information
ploegieku authored and dhimmel committed Dec 13, 2019
1 parent 3a328ff commit cbee423
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 148 deletions.
1 change: 0 additions & 1 deletion build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pandoc --verbose \
--csl="$CSL_PATH" \
--metadata link-citations=true \
--include-after-body=build/themes/default.html \
--include-after-body=build/plugins/table-scroll.html \
--include-after-body=build/plugins/anchors.html \
--include-after-body=build/plugins/accordion.html \
--include-after-body=build/plugins/tooltips.html \
Expand Down
12 changes: 5 additions & 7 deletions build/plugins/accordion.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,15 @@
function getHashTarget(link) {
const hash = link ? link.hash : window.location.hash;
const id = hash.slice(1);
let target = document.querySelector(
'[id="' + id + '"], [name="' + id + '"]'
);
let target = document.querySelector('[id="' + id + '"]');
if (!target)
return;

// if figure or table, modify target to get expected element
if (hash.indexOf('#fig:') === 0)
target = target.parentNode;
if (hash.indexOf('#tbl:') === 0)
target = target.nextElementSibling;
if (id.indexOf('fig:') === 0)
target = target.querySelector('figure');
if (id.indexOf('tbl:') === 0)
target = target.querySelector('table');

return target;
}
Expand Down
34 changes: 15 additions & 19 deletions build/plugins/anchors.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
const options = {
// which types of elements to add anchors next to, in
// "document.querySelector" format
typesQuery: 'h1, h2, h3, figure, table',
typesQuery: 'h1, h2, h3, [id^="fig:"], [id^="tbl:"], [id^="eq:"]',
// whether plugin is on or not
enabled: 'true'
};
Expand All @@ -50,36 +50,34 @@
function onScroll() {
// if url has hash and user has scrolled out of view of hash
// target, remove hash from url
const tolerance = 100;
const target = getHashTarget();
if (target) {
if (
target.getBoundingClientRect().top > window.innerHeight ||
target.getBoundingClientRect().bottom < 0
target.getBoundingClientRect().top >
window.innerHeight + tolerance ||
target.getBoundingClientRect().bottom < 0 - tolerance
)
history.pushState(null, null, ' ');
}
}

// add anchor to element
function addAnchor(element) {
let withId; // element with unique id
let addTo; // element to add anchor button to

// if figure or table, modify withId and addTo to get expected
// elements
if (element.tagName.toLowerCase() === 'figure') {
withId = element.querySelector('img');
if (element.id.indexOf('fig:') === 0) {
addTo = element.querySelector('figcaption');
} else if (element.tagName.toLowerCase() === 'table') {
withId =
element.previousElementSibling ||
element.parentNode.previousElementSibling;
} else if (element.id.indexOf('tbl:') === 0) {
addTo = element.querySelector('caption');
} else if (element.id.indexOf('eq:') === 0) {
addTo = element.querySelector('.eqnos-number');
}

withId = withId || element;
addTo = addTo || element;
const id = withId.id || withId.name || null;
const id = element.id || null;

// do not add anchor if element doesn't have assigned id.
// id is generated by pandoc and is assumed to be unique and
Expand All @@ -101,17 +99,15 @@
function getHashTarget() {
const hash = window.location.hash;
const id = hash.slice(1);
let target = document.querySelector(
'[id="' + id + '"], [name="' + id + '"]'
);
let target = document.querySelector('[id="' + id + '"]');
if (!target)
return;

// if figure or table, modify target to get expected element
if (hash.indexOf('#fig:') === 0)
target = target.parentNode;
if (hash.indexOf('#tbl:') === 0)
target = target.nextElementSibling;
if (id.indexOf('fig:') === 0)
target = target.querySelector('figure');
if (id.indexOf('tbl:') === 0)
target = target.querySelector('table');

return target;
}
Expand Down
12 changes: 5 additions & 7 deletions build/plugins/jump-to-first.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@

// add button next to each figure
function makeFigureButtons() {
const figures = document.querySelectorAll('img[id^="fig:"]');
const figures = document.querySelectorAll('[id^="fig:"]');
for (const figure of figures) {
// get figure id and element to add button to
const id = figure.id;
const container = figure.nextElementSibling;
const container = figure.querySelector('figcaption') || figure;
const first = getFirstOccurrence(id);

// if can't find link to figure, ignore
Expand All @@ -130,13 +130,11 @@

// add button next to each figure
function makeTableButtons() {
const tables = document.querySelectorAll('a[name^="tbl:"]');
const tables = document.querySelectorAll('[id^="tbl:"]');
for (const table of tables) {
// get ref id and element to add button to
const id = table.name;
const container = table.nextElementSibling.querySelector(
'caption'
);
const id = table.id;
const container = table.querySelector('caption') || table;
const first = getFirstOccurrence(id);

// if can't find link to table, ignore
Expand Down
10 changes: 1 addition & 9 deletions build/plugins/link-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,10 @@
function getHashTarget(link) {
const hash = link ? link.hash : window.location.hash;
const id = hash.slice(1);
let target = document.querySelector(
'[id="' + id + '"], [name="' + id + '"]'
);
let target = document.querySelector('[id="' + id + '"]');
if (!target)
return;

// if figure or table, modify target to get expected element
if (hash.indexOf('#fig:') === 0)
target = target.parentNode;
else if (hash.indexOf('#tbl:') === 0)
target = target.nextElementSibling.querySelector('caption');

return target;
}

Expand Down
68 changes: 0 additions & 68 deletions build/plugins/table-scroll.html

This file was deleted.

14 changes: 5 additions & 9 deletions build/plugins/tooltips.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,15 @@
function getSource(link) {
const hash = link ? link.hash : window.location.hash;
const id = hash.slice(1);
let target = document.querySelector(
'[id="' + id + '"], [name="' + id + '"]'
);
let target = document.querySelector('[id="' + id + '"]');
if (!target)
return;

// if figure or table, modify target to get expected element
if (hash.indexOf('#ref-') === 0)
// if ref or figure, modify target to get expected element
if (id.indexOf('ref-') === 0)
target = target.querySelector('p');
else if (hash.indexOf('#fig:') === 0)
target = target.parentNode;
else if (hash.indexOf('#tbl:') === 0)
return;
else if (id.indexOf('fig:') === 0)
target = target.querySelector('figure');

return target;
}
Expand Down
68 changes: 40 additions & 28 deletions build/themes/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
padding: 10px;
/* squash table if too wide for page by forcing line breaks */
overflow-wrap: break-word;
word-break: break-word;
}

/* header row and even rows */
Expand Down Expand Up @@ -617,6 +618,43 @@
margin-bottom: 20px;
}

/* -------------------------------------------------- */
/* tablenos */
/* -------------------------------------------------- */

/* tablenos wrapper */
.tablenos {
/* show scrollbar on tables if necessary to prevent overflow */
width: 100%;
margin: 20px 0;
}

.tablenos > table {
/* move margins from table to table_wrapper to allow margin collapsing */
margin: 0;
}

@media only screen {
/* tablenos wrapper */
.tablenos {
/* show scrollbar on tables if necessary to prevent overflow */
overflow-x: auto !important;
}

.tablenos th,
.tablenos td {
overflow-wrap: unset !important;
word-break: unset !important;
}

/* table in wrapper */
.tablenos table,
.tablenos table * {
/* don't break table words */
overflow-wrap: normal !important;
}
}

/* -------------------------------------------------- */
/* mathjax */
/* -------------------------------------------------- */
Expand Down Expand Up @@ -646,34 +684,8 @@

/* equation */
span[id^="eq:"] > span.math.display > span {
/* nudge to make room for equation auto-number */
margin-right: 40px !important;
}

/* -------------------------------------------------- */
/* table scroll plugin */
/* -------------------------------------------------- */

@media only screen {
/* table wrapper */
.table_wrapper {
/* show scrollbar on tables if necessary to prevent overflow */
overflow: auto;
width: 100%;
margin: 20px 0;
}

/* table within table wrapper */
.table_wrapper table,
.table_wrapper table * {
/* don't break table words */
word-break: normal !important;
}

.table_wrapper > table {
/* move margins from table to table_wrapper to allow margin collapsing */
margin: 0;
}
/* nudge to make room for equation auto-number and anchor */
margin-right: 60px !important;
}

/* -------------------------------------------------- */
Expand Down

0 comments on commit cbee423

Please sign in to comment.