-
Notifications
You must be signed in to change notification settings - Fork 5
Support multiple bibliographies per topic (etc.) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wlupton
wants to merge
3
commits into
pandoc-ext:main
Choose a base branch
from
wlupton:feature/misc-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
]] | ||
|
||
-- --citeproc was added in 2.11, so we never use the old pandoc-citeproc | ||
PANDOC_VERSION:must_be_at_least '2.11' | ||
|
||
local pandoc = require 'pandoc' | ||
|
@@ -32,21 +34,24 @@ local metatype = pandoc.utils.type or | |
|
||
--- Collection of all cites in the document | ||
local all_cites = {} | ||
|
||
--- Document meta value | ||
local doc_meta = pandoc.Meta{} | ||
|
||
--- Div used by citeproc to insert the bibliography. | ||
local refs_div = pandoc.Div({}, pandoc.Attr('refs')) | ||
|
||
--- 'references' metadata for each topic | ||
local topic_refs = {} | ||
|
||
-- Div filled by citeproc with properties set according to | ||
-- the output format and the attributes of cs:bibliography | ||
local refs_div_with_properties | ||
|
||
--- Run citeproc on a pandoc document | ||
-- | ||
-- Falls back to the external `pandoc-citeproc` filter if the built-in | ||
-- citeproc processor is not available. Tries to silence all citeproc | ||
-- warnings, which isn't possible in some versions. | ||
-- Tries to silence all citeproc warnings, which isn't possible in some | ||
-- versions. | ||
local citeproc = utils.citeproc | ||
if pcall(require, 'pandoc.log') and citeproc then | ||
-- silence all warnings if possible | ||
|
@@ -65,13 +70,28 @@ end | |
--- Resolve citations in the document by combining all bibliographies | ||
-- before running citeproc on the full document. | ||
local function resolve_doc_citations (doc) | ||
-- combine all bibliographies | ||
-- combine all bibliographies and references | ||
local meta = doc.meta | ||
local bibconf = meta.bibliography | ||
meta.bibliography = pandoc.MetaList{} | ||
if metatype(bibconf) == 'table' then | ||
for _, value in pairs(bibconf) do | ||
table.insert(meta.bibliography, stringify(value)) | ||
-- support list-valued items | ||
if metatype(value) ~= 'List' then value = List{value} end | ||
for _, val in ipairs(value) do | ||
table.insert(meta.bibliography, stringify(val)) | ||
end | ||
end | ||
end | ||
local refconf = meta.references | ||
meta.references = pandoc.MetaList{} | ||
if metatype(refconf) == 'table' then | ||
for topic, refs in pairs(refconf) do | ||
-- save topic references for meta_for_citeproc() | ||
topic_refs[topic] = refs | ||
for _, ref in ipairs(refs) do | ||
table.insert(meta.references, ref) | ||
end | ||
end | ||
end | ||
-- add refs div to catch the created bibliography | ||
|
@@ -80,30 +100,53 @@ local function resolve_doc_citations (doc) | |
doc = citeproc(doc) | ||
-- remove catch-all bibliography and keep it for future use | ||
refs_div_with_properties = table.remove(doc.blocks) | ||
-- restore bibliography to original value | ||
doc.meta.bibliography = orig_bib | ||
-- restore bibliography and references to original values | ||
doc.meta.bibliography = bibconf | ||
doc.meta.references = refconf | ||
return doc | ||
end | ||
|
||
--- Explicitly create a new meta object with all fields relevant for | ||
--- pandoc-citeproc. | ||
local function meta_for_pandoc_citeproc (bibliography) | ||
--- Explicitly create a new meta object with all fields relevant for citeproc. | ||
local function meta_for_citeproc (bibliography, topic) | ||
-- We could just indiscriminately copy all meta fields, but let's be | ||
-- explicit about what's important. | ||
local fields = { | ||
'bibliography', 'references', 'csl', 'citation-style', | ||
'link-citations', 'citation-abbreviations', 'lang', | ||
'suppress-bibliography', 'reference-section-title', | ||
'notes-after-punctuation', 'nocite' | ||
'notes-after-punctuation', 'nocite', 'link-bibliography' | ||
} | ||
local new_meta = pandoc.Meta{} | ||
for _, field in ipairs(fields) do | ||
new_meta[field] = doc_meta[field] | ||
local value = doc_meta[field] | ||
-- replace 'references' with the topic references | ||
if field == 'references' and metatype(value) == 'table' and topic then | ||
value = topic_refs[topic] | ||
end | ||
new_meta[field] = value | ||
end | ||
new_meta.bibliography = bibliography | ||
return new_meta | ||
end | ||
|
||
-- list of ref-xxx identifiers that have already been output | ||
local identifiers = List() | ||
|
||
-- ignore duplicate references (the first definition will win) | ||
local function ignore_duplicates(blocks) | ||
local new_blocks = pandoc.Blocks{} | ||
for _, block in ipairs(blocks) do | ||
local identifier = block.attr.identifier | ||
if not identifiers:includes(identifier) then | ||
local new_block = pandoc.walk_block(block, {Span=_span}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line doesn't do anything, as |
||
new_blocks:insert(new_block) | ||
identifiers:insert(identifier) | ||
end | ||
end | ||
|
||
return new_blocks | ||
end | ||
|
||
local function remove_duplicates(classes) | ||
local seen = {} | ||
return classes:filter(function(x) | ||
|
@@ -126,12 +169,12 @@ local function create_topic_bibliography (div) | |
return nil | ||
end | ||
local tmp_blocks = {pandoc.Para(all_cites), refs_div} | ||
local tmp_meta = meta_for_pandoc_citeproc(bibfile) | ||
local tmp_meta = meta_for_citeproc(bibfile, name) | ||
local tmp_doc = pandoc.Pandoc(tmp_blocks, tmp_meta) | ||
local res = citeproc(tmp_doc) | ||
local res = citeproc(tmp_doc, true) | ||
-- First block of the result contains the dummy paragraph, second is | ||
-- the refs Div filled by citeproc. | ||
div.content = res.blocks[2].content | ||
div.content = ignore_duplicates(res.blocks[2].content) | ||
-- Set the classes and attributes as citeproc did it on refs_div | ||
div.classes = remove_duplicates(refs_div_with_properties.classes) | ||
div.attributes = refs_div_with_properties.attributes | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A somewhat shorter implementation could be created when using
pandoc.List.filter
, e.g.