Skip to content

Commit

Permalink
renamed extension name to add-code-files
Browse files Browse the repository at this point in the history
  • Loading branch information
shafayetShafee committed Feb 11, 2023
1 parent 5100c34 commit 6ce9964
Show file tree
Hide file tree
Showing 39 changed files with 5,438 additions and 5,860 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
# Include Code Files Extension For Quarto
# Add Code Files Extension For Quarto

This extension provides filter that to include code from source files.
This extension provides filter that to add code from source files.

:information_source: This filter is an alternative to the [include-code-files](https://github.com/quarto-ext/include-code-files) by [@quarto-ext](https://github.com/quarto-ext) with [`code-fold`](https://quarto.org/docs/output-formats/html-code.html#folding-code)-ing capability. If you just want to include contents from another file and do not care about code-folding you probably want to use the former filter, which is simpler to use.
:information_source: This filter is an alternative to the [include-code-files](https://github.com/quarto-ext/include-code-files) by [@quarto-ext](https://github.com/quarto-ext) with [`code-fold`](https://quarto.org/docs/output-formats/html-code.html#folding-code)-ing capability. If you just want to add contents from another file and do not care about code-folding you probably want to use the former filter, which is simpler to use.

## Installing

:warning: This extension requires quarto version at least to be 1.2.

```bash
quarto add shafayetShafe/include-code-files
quarto add shafayetShafe/add-code-files
```

This will install the extension under the `_extensions` subdirectory.
If you're using version control, you will want to check in this directory.

## Usage

The filter recognizes [Divs](https://quarto.org/docs/authoring/markdown-basics.html#divs-and-spans) or code-chunk with the `include-from` attribute present. It swaps the content of the code block with contents from a file.
The filter recognizes [Divs](https://quarto.org/docs/authoring/markdown-basics.html#divs-and-spans) or code-chunk with the `add-from` attribute present. It swaps the content of the code block with contents from a file.

Here is how you add the filter to a quarto document,

```
---
title: "Quarto"
filters:
- include-code-files
- add-code-files
---
```

### Including Files

Once adding the filter to the quarto document, use the `include-from` attribute in a Div containing a empty code-block. For example,
Once adding the filter to the quarto document, use the `add-from` attribute in a Div containing a empty code-block. For example,

````
::: {include-from=hello-world.cpp}
::: {add-from=hello-world.cpp}
```{.cpp}
```
:::
````

would include the codes from `hello-world.cpp` within that `.cpp` code block. And you need to use `.cpp` to get correct syntax highlighting for added c++ code. Run `quarto pandoc --list-highlight-languages` to get the list of languages for which syntax highlighting is supported.
would add the codes from `hello-world.cpp` within that `.cpp` code block. And you need to use `.cpp` to get correct syntax highlighting for added c++ code. Run `quarto pandoc --list-highlight-languages` to get the list of languages for which syntax highlighting is supported.

You can also use the following options,

- **`start-line`**, **`end-line`**: To include a specific range of lines.
- **`start-line`**, **`end-line`**: To add a specific range of lines.
- **`code-line-numbers`**: To enable source code line numbering.
- **dedent**: using this you can have whitespaces removed on each line, where possible (non-whitespace character will not be removed even if they occur in the dedent area).

````
::: {include-from=hello-world.cpp start-line=1 end-line=8 code-line-numbers="true"}
::: {add-from=hello-world.cpp start-line=1 end-line=8 code-line-numbers="true"}
```{.cpp}
```
:::
````

View the live demo of

- [document rendered with `jupyter`](https://shafayetshafee.github.io/include-code-files/example.html)
- [document rendered with `jupyter`](https://shafayetshafee.github.io/add-code-files/example.html)

- [document rendered with `knitr`](https://shafayetshafee.github.io/include-code-files/example_knitr.html)
- [document rendered with `knitr`](https://shafayetshafee.github.io/add-code-files/example_knitr.html)

### filename and code-filename

You can also use the `filename` attribute to show a name of the file the included code is associated with. But the issue is, the attribute `filename` does not work with `code-folding` as intended for that code block. This filter provides another option `code-filename` which works with `code-folding`.
You can also use the `filename` attribute to show a name of the file the added code is associated with. But the issue is, the attribute `filename` does not work with `code-folding` as intended for that code block. This filter provides another option `code-filename` which works with `code-folding`.

Therefore, use `code-filename` only when using `code-fold: true`, otherwise use `filename` (for non HTML format or for html format without `code-fold: true`)

**`code-filename` only works with `code-folding`. For othercases, use `filename`**

View a [live demo of this issue](https://shafayetshafee.github.io/include-code-files/example_filename.html)
View a [live demo of this issue](https://shafayetshafee.github.io/add-code-files/example_filename.html)

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
title: Include Code Files
title: Add Code Files
authors:
- name: quarto-ext
- name: Shafayet Khan Shafee
version: 1.0.0
version: 1.1.0
quarto-required: ">=1.2"
contributes:
filters:
- include-code-files.lua
- add-code-files.lua


Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ local str = pandoc.utils.stringify

local function ensureHtmlDeps()
quarto.doc.add_html_dependency({
name = "include-code-files",
name = "add-code-files",
version = "1.0.0",
scripts = {
{ path = "resources/js/include-code-files.js", afterBody = true}
{ path = "resources/js/add-code-files.js", afterBody = true}
},
stylesheets = {"resources/css/include-code-files.css"}
stylesheets = {"resources/css/add-code-files.css"}
})
end

Expand Down Expand Up @@ -68,8 +68,8 @@ end
-- gets the necessary chunk option and apply the source_include function with
-- these.
function Div(el)
if el.attributes['include-from'] then
local filepath = str(el.attributes['include-from'])
if el.attributes['add-from'] then
local filepath = str(el.attributes['add-from'])
local startLine = el.attributes['start-line']
local endLine = el.attributes['end-line']
local dedent_line = el.attributes.dedent
Expand Down
Loading

0 comments on commit 6ce9964

Please sign in to comment.