Skip to content
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

Added an option for hiding comments from code chunk #3

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.html
*.pdf
*_files/
.Rproj.user
*.rproj
.Rhistory
!docs/*
48 changes: 43 additions & 5 deletions _extensions/code-visibility/code-visibility.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


local str = pandoc.utils.stringify
local p = quarto.log.output

-- remove any lines with the hide_line directive.
function CodeBlock(el)
Expand All @@ -11,6 +11,46 @@ function CodeBlock(el)
end
end

-- function to check whether a value exists in a table
function has_value(table, value)
for k, v in ipairs(table) do
if v == value then
return true
end
end
return false
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 = "^" .. 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

-- apply filter_stream directive to cells
function Div(el)
if el.classes:includes("cell") then
Expand All @@ -30,13 +70,11 @@ function Div(el)
end
end
})

end

end

end


function filter_lines(text, filter)
local lines = pandoc.List()
local code = text .. "\n"
Expand Down
50 changes: 50 additions & 0 deletions example.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "Code Visibility"
author: Shafayet Khan Shafee
date: last-modified
format: html
comment-directive:
- "#>"
- "#>"
filters:
- code-visibility
execute:
echo: true
eval: false
---

## R

```{r}
#| message: false
#> This is some hidden comment which is visible in code editor
#> but not in rendered documents
library(dplyr)

print("#>") #| hide_line
#>
#> more hidden comment
mtcars %>%
select(mpg, am ,vs) %>%
group_by(vs) %>%
summarise(mean(mpg))
```

## Javascript

```{js}
//> This is some hidden comment which is visible in code editor
//> but not in rendered documents

viewof bill_length_min = Inputs.range(
[32, 50],
{value: 35, step: 1, label: "Bill length (min):"}
)
viewof islands = Inputs.checkbox(
["Torgersen", "Biscoe", "Dream"],
{ value: ["Torgersen", "Biscoe"],
label: "Islands:"
}
)
```