Skip to content

Commit

Permalink
initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
shafayetShafee committed Feb 11, 2023
0 parents commit 2187f45
Show file tree
Hide file tree
Showing 11 changed files with 25,766 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.html
*.pdf
*_files/
!docs/*
.Rproj.user
*.rproj
21 changes: 21 additions & 0 deletions LICENSE
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.
70 changes: 70 additions & 0 deletions README.md
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,

![](sshot/hide-comment-example-ss.png)

## 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).
8 changes: 8 additions & 0 deletions _extensions/hide-comment/_extension.yml
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

81 changes: 81 additions & 0 deletions _extensions/hide-comment/hide-comment.lua
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


Loading

0 comments on commit 2187f45

Please # to comment.