-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Custom environment using Fenced Divs #940
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
Merged
Merged
Changes from all commits
Commits
Show all changes
96 commits
Select commit
Hold shift + click to select a range
5130454
Correctly detect supported custom class
cderv f184c09
refactor debuging helper
cderv c693e43
Get the label for reference from div identifier
cderv 995f8a4
label should be local to the program
cderv 0be16a1
Create the latex environment based on the fenced_divs values.
cderv 35b4bab
insert the part to allow reference
cderv 3158734
make it conditional to label existance.
cderv 7322714
test empty rather than nil for id
cderv 965c7aa
first working version for HTML
cderv da0a20b
directly write raw tex to not rely on resolve_refs_latex
cderv 773943a
remove trailing space
cderv d91cb6f
For now warn is no id is set
cderv 5764b9f
correct alignment
cderv 70c70e7
add the supported custom environments
cderv 50e7183
we need rmarkdown dev
cderv c1410ec
Refactor to clarify
cderv b1d6ce5
do not separate in two rawblock
cderv 4c18841
missing string.format
cderv cebd653
also add the environment definition in tex header if only pandoc fenc…
cderv 53e1700
add rough support for eng_proof
cderv af25c92
Correctly get the content on the same line as Proof.
cderv d18d277
Find a way to skip if it is a block already processed by eng_proof
cderv 43b2586
Add the dot after the provided name
cderv 473e131
Add the space only if name if provided
cderv d696340
do not rely on position and check all inlines
cderv 302a2ab
keep the id on the span even for proof engine
cderv 0caa2eb
correct test for warning
cderv 16131f3
Pass _bookdown.yml to pandoc
cderv 31e76c7
can't index a nil value in Lua
cderv a45ed6b
Really don't need the pre_processor.
cderv 85562c1
add pandoc lua filters helpers functions
cderv 0cc8320
add custom environment lua filters by default for now in gitbook pdf_…
cderv bc91db0
some garbage from previous tests
cderv 883d27a
obviously only pass as metadata-file if _bookdown.yml exists
cderv 586782b
reword
cderv d421a5c
ignore data-latex attributes so that latex-divs.lua in rmarkdown does…
cderv dfc22e4
= not ==
cderv 65eccba
use a temp yml file to avoid conflict with other pandoc metadata
cderv 019b049
Require in dev rmarkdown version currently a specific PR
cderv 29dd63e
no need to create multiple file. Use only one that is overriden
cderv 4070303
add tests for custom_bookdown_filters
cderv 6d9b11b
separate the lua filter to work with new rmarkdown support
cderv 89944d2
prepend lua filter in html_document2
cderv 833435c
Correct message
cderv 99c1ce8
add a wrapper and add in several formats
cderv 9bda2da
also in html_chapters()
cderv 53130ac
remove undesired changes
cderv 566bc7a
add test for other helper
cderv 7b6ac0a
Fix the debugging printing
cderv 3d11832
ignore explictly divs created by eng_theorem too
cderv 739f4e5
improve debugging messages to help testing and checking
cderv 409b571
unused variable
cderv d790a83
Add to first content of the Div, not first in the Div
cderv eaf349c
but there is a space missing
cderv 351b1f9
forgot to remove one parenthesis
cderv 8f3411d
bookdown use = not <-
cderv 3d31abf
Use name as the current engines
cderv ffa2069
add a function to convert theorems and proofs engine to pandoc fenced…
cderv 3440515
use also label regex
cderv 3876efe
Add a test for convert function
cderv 24295ef
Do not rewrite the file if no chunk to modify
cderv bc3bb62
change message
cderv e4dfb59
it is the sum of logical to get the number
cderv 228711d
generate an id if none is provided
cderv 21d63be
add the square after the last p tag
cderv f68c2c3
refactor and simplify the lua filter
cderv 812f4b6
add a commment to better explain
cderv 5012125
clean the lua filter
cderv 6c19437
Import rmarkdown dev version
cderv 8812e1c
all_math_env is what we want here
cderv e7976ea
add some test for Theorems and Proofs with fenced divs
cderv 9d54de0
load config is not exported
cderv 802e1e9
use inherits() from base instead of is() from methods
cderv 7a49610
it does not work currently as with testthat and won't load the all pa…
cderv 30d14e6
test _bookdown.yml is in rmd dir
cderv 683df63
separate the tests
cderv 4e7e764
Use pandoc.Attr constructor instead of the more convenient attribut s…
cderv da703ff
review help page
cderv fe318f7
typo
cderv 8f47d00
Simplify the nested if structure
cderv db731db
use local functions instead of global
cderv 5a90518
Defines get_name function locally but outside of Div function and sim…
cderv 4e97a1e
rmarkdown 2.4 is on CRAN now
yihui 04fbf45
cosmetic
yihui 5e51337
tweak convert_to_fenced_div() and rename it to fence_theorems()
yihui 6215f07
add a `text` argument to make this function easier to test (no need t…
yihui cf36699
move the lua folder from resources/ to rmarkdown/ so we can use rmark…
yihui 13fac33
only add tests that are directly relevant to bookdown, and not tests …
yihui 4d7a123
return() early to make the code more compact (less indent and fewer n…
yihui 6ef5c31
make the config and the temp path arguments of the function, so the f…
yihui 8b598b4
the actual filename shouldn't matter
yihui bc7f09e
rename bookdown_metadata_file_arg() to bookdown_yml_arg()
yihui 754ee46
this test won't work with rmarkdown 2.4 because of https://github.com…
yihui 5233ec6
simplify the tests for bookdown_yml_arg() after faa25ee27edd88d683411…
yihui 7c5fbb8
add the custom-environment.lua filter in common_format_config() inste…
yihui f613761
add a news item, and we can close #924 now
yihui 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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| --[[ | ||
| A Pandoc 2 lua filter to deal with custom environment in bookdown | ||
| --]] | ||
|
|
||
| -- theorem types available to be used | ||
| local theorem_abbr = { | ||
| theorem = 'thm', | ||
| lemma = 'lem', | ||
| corollary = 'cor', | ||
| proposition = 'prp', | ||
| conjecture = 'cnj', | ||
| definition = 'def', | ||
| example = 'exm', | ||
| exercise = 'exr' | ||
| } | ||
|
|
||
| -- other special proof envs | ||
| local proof_label = { | ||
| proof = 'Proof', | ||
| remark = 'Remark', | ||
| solution = 'Solution' | ||
| } | ||
|
|
||
| -- for debuging purpose | ||
| local debug_mode = os.getenv("DEBUG_PANDOC_LUA") == "TRUE" | ||
| local function print_debug(label,obj,iter) | ||
| obj = obj or nil | ||
| iter = iter or pairs | ||
| label = label or "" | ||
| label = "DEBUG (from custom-environment.lua): "..label | ||
| if (debug_mode) then | ||
| if not obj then | ||
| print(label) | ||
| elseif (type(obj) == "string") then | ||
| print(label.." "..obj) | ||
| elseif type(obj) == "table" then | ||
| for k,v in iter(obj) do | ||
| print(label.."id:"..k.. " val:"..v) | ||
| end | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| -- create a unique id for a div with none provided | ||
| local counter = 0 | ||
| local function unlabeled_div() | ||
| counter = counter + 1 | ||
| return "unlabeled-div-"..(counter) | ||
| end | ||
|
|
||
| -- return [name] for latex, and (name) for html | ||
| local function get_name(format, options) | ||
| local name = options["name"] | ||
| if not name then return "" end | ||
| local template = {latex = "[%s]", html = " (%s)"} | ||
| name = string.format(template[format], name) | ||
| print_debug("name -> ", name) | ||
| -- remove data-name from option | ||
| options["name"] = nil | ||
| return name | ||
| end | ||
|
|
||
| -- Get metadata specific to bookdown for this filter | ||
| Meta = function(m) | ||
| bookdownmeta = m.bookdown | ||
| if (bookdownmeta and bookdownmeta.language and bookdownmeta.language.label) then | ||
| -- For internationalization feature of bookdown | ||
| for k,v in pairs(bookdownmeta.language.label) do | ||
| if (type(v) == 'table' and v.t == 'MetaInlines' and proof_label[k] ~= nil) then | ||
| -- remove any undesired space (3 or less) | ||
| proof_label[k] = pandoc.utils.stringify(v):gsub("%.?%s?%s?%s?$", "") | ||
| print_debug("Translation-> "..k..":", proof_label[k]) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| -- Modify Pandoc AST for supported custom environment | ||
| Div = function (div) | ||
| local classes = div.classes | ||
| -- we do nothing if no classes | ||
| if (#classes == 0) then | ||
| print_debug("No classes in the Div.") | ||
| return div | ||
| end | ||
| print_debug("Div classes -> " , classes) | ||
|
|
||
| -- checking if the class is one of the supported custom environment | ||
| local env_type = {type = nil, env = nil} | ||
| for i,v in ipairs(classes) do | ||
| if (theorem_abbr[v] ~= nil) then | ||
| env_type.type = "theorem" | ||
| env_type.env = v | ||
| break | ||
| elseif (proof_label[v] ~= nil) then | ||
| env_type.type = "proof" | ||
| env_type.env = v | ||
| break | ||
| end | ||
| end | ||
| -- classes is not a supported one, we return as is | ||
| if not env_type.env then | ||
| print_debug("Not a bookdown supported custom class") | ||
| return div | ||
| end | ||
| print_debug("Found types -> ", env_type) | ||
|
|
||
| -- get the id if it exists - it will we use to build label for reference | ||
| local id = div.identifier | ||
| -- if no id, one is generated so that bookdown labelling mechanism works | ||
| if #id == 0 then id = unlabeled_div() end | ||
| print_debug("id -> ", id) | ||
| -- remove unwanted identifier on the div, as it will be on the span | ||
| div.identifier = "" | ||
|
|
||
| -- get the attributes | ||
| local options = div.attributes | ||
| if (options["data-latex"] ~= nil) then | ||
| -- so that latex-divs.lua in rmarkdown does not activate | ||
| print("[WARNING] data-latex attribute can't be used with one of bookdown custom environment. It has been removed.") | ||
| options["data-latex"] = nil | ||
| end | ||
|
|
||
| -- create the custom environment | ||
| local label | ||
| -- Create a label for referencing - only for theorem like env | ||
| if (env_type.type == "theorem") then | ||
| label = string.format("%s:%s", theorem_abbr[env_type.env], id) | ||
| end | ||
| print_debug("label for reference -> ", label) | ||
|
|
||
| -- TODO: should we support beamer also ? | ||
cderv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (FORMAT:match 'latex') then | ||
| local label_part | ||
| if label then | ||
| label_part = string.format( "\n\\protect\\hypertarget{%s}{}\\label{%s}", label, label) | ||
| end | ||
| local name = get_name('latex', options) | ||
| table.insert( | ||
| div.content, 1, | ||
| pandoc.RawBlock('tex', string.format('\\begin{%s}%s%s', env_type.env, name, label_part or "")) | ||
| ) | ||
| table.insert( | ||
| div.content, | ||
| pandoc.RawBlock('tex', string.format('\\end{%s}', env_type.env)) | ||
| ) | ||
| elseif (FORMAT:match 'html') then | ||
| local name = get_name('html', options) | ||
|
|
||
| -- if div is already processed by eng_theorem, it would also modify it. | ||
| -- we can ignore knowing how eng_theorem modifies options$html.before2 | ||
| -- It can be Plain or Para depending if a name was used or not. | ||
| -- MAYBE NOT VERY RELIABLE THOUGH | ||
| if (div.content[1].t == "Plain" or div.content[1].t == "Para") then | ||
| for i,el in pairs(div.content[1].content) do | ||
| if (el.t == "Span" and el.classes[1] == env_type.env) then | ||
| print_debug("Already processed by knitr engine.") | ||
| return div | ||
| end | ||
| end | ||
| end | ||
|
|
||
| -- inserted the correct span depending on the environment type | ||
| local span | ||
| if (env_type.type == "theorem") then | ||
| span = pandoc.Span( | ||
| pandoc.Strong(string.format("(#%s)%s ", label, name)), | ||
| pandoc.Attr(label, {env_type.env}) | ||
| ) | ||
| elseif (env_type.type == "proof") then | ||
| span = pandoc.Span({ | ||
| pandoc.Emph(pandoc.Str(proof_label[env_type.env])), | ||
| pandoc.Str(name), | ||
| pandoc.Str("."), | ||
| pandoc.Space() | ||
| }, | ||
| pandoc.Attr(id, {env_type.env}) | ||
| ) | ||
| end | ||
| -- add to the first block of the div, and not as first block | ||
| table.insert(div.content[1].content, 1, span) | ||
| end | ||
|
|
||
| return div | ||
| end | ||
|
|
||
| return {{Meta = Meta}, {Div = Div}} | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| language: | ||
| label: | ||
| solution: 'SOLUTION' |
Oops, something went wrong.
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.
This has been addressed by c59e3dc.
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.
I kind of remember having tried that, and that was not ok in the output... I may have wrongly test it maybe. 🤷♂️