Add tabbed Julia/Python language switcher to documentation code blocks#89
Add tabbed Julia/Python language switcher to documentation code blocks#89
Conversation
…hon code tabs Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/0727b57a-dd85-44ce-b97a-0827797e7681 Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/0727b57a-dd85-44ce-b97a-0827797e7681 Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
darnstrom
left a comment
There was a problem hiding this comment.
Would it be possible to use the added icons for julia and python in docs/src/assets for the different code sections? Also, would it be possible to somehow also run have the Python alternative also for @example? The code that is actually run by Documenter only have to be the Julia version, but it would be nice to have the Julia alternative.
Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/1254071e-6908-4d07-9971-6d21e98db741 Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
…ation in @example blocks Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/5b603aea-a2ea-41f8-88f4-7bc291559495 Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
Addressed in commit Two new fenced-code block types are introduced: |
darnstrom
left a comment
There was a problem hiding this comment.
Now the make.jl does not build:
Make sure that the new functionality builds!
julia> include("make.jl")
ERROR: LoadError: FieldError: type SubString has no field captures, available fields: string, offset, ncodeunits
Stacktrace:
[1] getproperty
@ ./Base_compiler.jl:54 [inlined]
[2] (::var"#_expand_tab_blocks##12#_expand_tab_blocks##13")(m::SubString{String})
@ Main ~/code/LinearMPC.jl/docs/make.jl:87
[3] _replace(io::IOBuffer, repl::var"#_expand_tab_blocks##12#_expand_tab_blocks##13", str::String, r::UnitRange{…}, pattern::Base.RegexAndMatchData)
@ Base ./strings/util.jl:947
[4] _replace_finish(io::IOBuffer, str::String, count::Int64, e1::Int64, patterns::Tuple{…}, replaces::Tuple{…}, rs::Tuple{…})
@ Base ./strings/util.jl:990
[5] replace(str::String, pat_repl::Tuple{Pair{Regex, var"#_expand_tab_blocks##12#_expand_tab_blocks##13"}}, count::Int64)
@ Base ./strings/util.jl:1043
[6] replace
@ ./strings/util.jl:1096 [inlined]
[7] _expand_tab_blocks(content::String)
@ Main ~/code/LinearMPC.jl/docs/make.jl:85
[8] top-level scope
@ ~/code/LinearMPC.jl/docs/make.jl:114
[9] include(mapexpr::Function, mod::Module, _path::String)
@ Base ./Base.jl:307
[10] top-level scope
@ REPL[1]:1
in expression starting at /home/darnstrom/code/LinearMPC.jl/docs/make.jl:108
Some type information was truncated. Use show(err) to see complete types.
…cess regex captures Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/db10d7ba-5a52-4564-84b5-28b762d421d3 Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
Fixed in commit |
darnstrom
left a comment
There was a problem hiding this comment.
There are still some code parts that uses the raw html tag to get the switching between Julia and Python (mainly the parts which do not include the examples now.) Could you also make sure that they are made more "maintainable" similar to what you just did for the @tabexample and @tabsetup
Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/f93d3479-a4b1-4c56-85be-3217fac5a86a Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
Agent-Logs-Url: https://github.com/darnstrom/LinearMPC.jl/sessions/8835be11-cc55-4d3e-8590-83e7c6e5eb33 Co-authored-by: darnstrom <55484604+darnstrom@users.noreply.github.com>
Done in commit |


Documentation only had Julia code examples; the Python
lmpcpackage provides equivalent functionality but was undiscoverable from the docs.Changes
Infrastructure
docs/src/assets/lang-switcher.css— Tab button + panel styles using CSS custom properties to inherit Documenter.jl's theme; usesinline-flexwith language iconsdocs/src/assets/lang-switcher.js— Tab switching withlocalStoragepreference persistence and page-level sync across all switchers; triggershljs.highlightElement()on code in raw HTML blocksdocs/make.jl— Wires assets in viaDocumenter.HTML(assets=[...]); contains a markdown preprocessor that expands@tab/@tabexample/@tabsetupblocks beforemakedocsruns and restores source files in atry/finallyMaintainable custom block types
Three custom fenced-code block types are introduced and processed by the
docs/make.jlpreprocessor:@tab— Display-only tab switcher (no code execution). Julia and Python code written once per block; expands to the full HTML lang-switcher.@tabexample name— Julia code written once: shown in the Julia tab AND executed via@example(all lines auto-hidden so only output such as plots is shown). An optional# exec-onlysection handles lines that execute but are not shown to users (e.g.ylims!for docs plot sizing).@tabsetup name— Julia code written once: shown in the Julia tab AND executed via@setup(silently, no output). Shares state with subsequent@tabexample nameblocks of the same name.All three block types follow the same syntax: