-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2187f45
Showing
11 changed files
with
25,766 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
*.html | ||
*_files/ | ||
!docs/* | ||
.Rproj.user | ||
*.rproj |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Shafayet Khan Shafee | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,70 @@ | ||
# Hide-comment Extension For Quarto | ||
|
||
`hide-comment` is a [Quarto](https://quarto.org/) filter extension that provides comment-directive to hide comment from code chunk in the rendered document (works for html/revealjs/pdf format.) | ||
|
||
## Installing | ||
|
||
:warning: This extension requires Quarto version to be at least 1.2. | ||
|
||
```bash | ||
quarto add shafayetShafee/hide-comment | ||
``` | ||
|
||
This will install the extension under the `_extensions` subdirectory. | ||
If you're using version control, you will want to check in this directory. | ||
|
||
## Using | ||
|
||
At first, use the filter in the document yaml section, | ||
|
||
``` | ||
filters: | ||
- hide-comment | ||
``` | ||
|
||
Now to write a comment that will be hidden from the code chunk in rendered document, you need to use comment-directives. And the syntax of the comment directive depends on the language of the code chunk and therefore, should start with the character(s) that is used to do inline commenting in that programming language. | ||
|
||
So for python, R and Julia, it should start with hash or pound symbol `#`. For javascript or ojs or dot chunk, it should start with `//`, for mermaid graph chunk it should start with `%%`. It is because, after all, they should be valid comment line when the document parses. | ||
|
||
After deciding on the syntax of comment-directives, enlist them with or under the `comment-directive` yaml option. Then `hide-comment` filter will remove the lines from code chunk that initiates with the enlisted directives. | ||
|
||
A simple example as follows, | ||
|
||
~~~ | ||
--- | ||
title: "hide-comment example" | ||
comment-directive: "#>" | ||
filters: | ||
- hide-comment | ||
code-line-numbers: true | ||
--- | ||
```{r} | ||
#| message: false | ||
#> This is some hidden comment which is visible in code editor | ||
#> but not in rendered documents | ||
library(dplyr) | ||
#> | ||
#> more hidden comment | ||
mtcars %>% | ||
select(mpg, am ,vs) %>% | ||
group_by(vs) %>% | ||
summarise(mean(mpg)) | ||
``` | ||
~~~ | ||
|
||
|
||
And the rendered version, | ||
|
||
 | ||
|
||
## Example | ||
|
||
Here is the source code for example: [example.qmd](example.qmd) and [the rendered output](https://shafayetshafee.github.io/hide-comment/example.html) | ||
|
||
|
||
## Acknowledgement | ||
|
||
This extension is largely inspired by another quarto extension [`code-visibility`](https://github.com/jjallaire/code-visibility). |
This file contains 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,8 @@ | ||
title: Hide-comment | ||
author: Shafayet Khan Shafee | ||
version: 1.0.0 | ||
quarto-required: ">=1.2.0" | ||
contributes: | ||
filters: | ||
- hide-comment.lua | ||
|
This file contains 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,81 @@ | ||
--[[ | ||
MIT License | ||
Copyright (c) 2023 Shafayet Khan Shafee | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
]]-- | ||
|
||
|
||
local str = pandoc.utils.stringify | ||
-- local p = quarto.log.output | ||
|
||
|
||
-- `filter_lines` taken from https://github.com/jjallaire/code-visibility | ||
function filter_lines(text, filter) | ||
local lines = pandoc.List() | ||
local code = text .. "\n" | ||
for line in code:gmatch("([^\r\n]*)[\r\n]") do | ||
if filter(line) then | ||
lines:insert(line) | ||
end | ||
end | ||
return table.concat(lines, "\n") | ||
end | ||
|
||
-- create escaped cmnt_directive_pattern | ||
function escape_pattern(s) | ||
local escaped = "" | ||
for c in s:gmatch(".") do | ||
escaped = escaped .. "%" .. c | ||
end | ||
return escaped | ||
end | ||
|
||
-- function for applying comment_directive | ||
local function apply_cmnt_directives(comment_directive) | ||
local line_filter = { | ||
CodeBlock = function(cb) | ||
if cb.classes:includes('cell-code') then | ||
for k, cd in ipairs(comment_directive) do | ||
-- local cmnt_directive_tbl = {"#>", "//>"} | ||
-- if has_value(cmnt_directive_tbl, str(cd)) then | ||
local cmnt_directive_pattern = "^" .. escape_pattern(str(cd)) | ||
cb.text = filter_lines(cb.text, function(line) | ||
return not line:match(cmnt_directive_pattern) | ||
end) | ||
-- end | ||
end | ||
return cb | ||
end | ||
end | ||
} | ||
return line_filter | ||
end | ||
|
||
-- hide lines with comment directive | ||
function Pandoc(doc) | ||
local meta = doc.meta | ||
local cd = meta['comment-directive'] | ||
if cd then | ||
return doc:walk(apply_cmnt_directives(cd)) | ||
end | ||
end | ||
|
||
|
Oops, something went wrong.