Skip to content

Commit

Permalink
fix: weird tangle file path logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas committed Nov 17, 2024
1 parent c7ada78 commit 752bc97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
9 changes: 3 additions & 6 deletions lua/neorg/modules/core/dirman/utils/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ local module = neorg.modules.create("core.dirman.utils")
module.public = {
---Resolve `$<workspace>/path/to/file` and return the real path
---@param path string | PathlibPath # path
---@param raw_path boolean? # If true, returns resolved path, otherwise, returns resolved path
---and append ".norg"
---@param host_file string | PathlibPath | nil file the link resides in, if the link is
---relative, this file is used instead of the current file
---@return PathlibPath?, boolean? # Resolved path. If path does not start with `$` or not absolute, adds
---relative from current file.
---@param raw_path boolean? # If true, returns resolved path, otherwise, returns resolved path and append ".norg"
---@param host_file string | PathlibPath | nil file the link resides in, if the link is relative, this file is used instead of the current file
---@return PathlibPath?, boolean? # Resolved path. If path does not start with `$` or not absolute, adds relative from current file.
expand_pathlib = function(path, raw_path, host_file)
local relative = false
if not host_file then
Expand Down
29 changes: 22 additions & 7 deletions lua/neorg/modules/core/tangle/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The `tangle` module currently provides a single command:
- `:Neorg tangle current-file` - performs all possible tangling operations on the current file
### Usage Tutorial
By default, *zero* code blocks are tangled. You must provide where you'd like to tangle each code
block manually (global configuration will be discussed later). To do so, add a `#tangle
<output-file>` tag above the code block you'd wish to export, where <output-file> is relative to the
Expand All @@ -26,6 +27,10 @@ print("Hello World!")
```
The above snippet will *only* tangle that single code block to the desired output file: `init.lua`.
> [!WARNING]
> Due to a bug in the norg treesitter parser, `#tangle ./init.lua` or `#tangle folder/init.lua` will not work
> As a result, we recommend specifying files destinations in metadata
#### Global Tangling for Single Files
Apart from tangling a single or a set of code blocks, you can declare a global output file in the document's metadata:
```norg
Expand All @@ -51,6 +56,17 @@ tangle: [
The above snippet tells the Neorg tangling engine to tangle all `lua` code blocks to `./init.lua` and all `haskell` code blocks to `./output.hs`.
As always if any of the code blocks have a `#tangle` tag then that takes precedence.
If you want to be more verbose, or you're tangling to a file without an extension (perhaps you're
writing a shell script that has a shebang) you can also do this:
```norg
@document.meta
tangle: {
lua: ./init.lua
python: ./output
}
@end
```
#### Ignoring Code Blocks
Sometimes when tangling you may want to omit some code blocks. For this you may use the `#tangle.none` tag:
```norg
Expand Down Expand Up @@ -166,17 +182,21 @@ local lib, modules, utils, log = neorg.lib, neorg.modules, neorg.utils, neorg.lo

local module = modules.create("core.tangle")
local Path = require("pathlib")
---@type core.dirman.utils
local dirman_utils

module.setup = function()
return {
requires = {
"core.integrations.treesitter",
"core.dirman.utils",
"core.neorgcmd",
},
}
end

module.load = function()
dirman_utils = module.required["core.dirman.utils"]
modules.await("core.neorgcmd", function(neorgcmd)
neorgcmd.add_commands_from_table({
tangle = {
Expand Down Expand Up @@ -477,14 +497,9 @@ module.on_event = function(event)
local tangled_count = 0

for file, content in pairs(tangles) do
-- resolve upward relative path like `../../`
local relative_file, upward_count = string.gsub(file, "%.%.[\\/]", "")
if upward_count > 0 then
local base_dir = vim.fn.expand("%:p" .. string.rep(":h", upward_count + 1)) --[[@as string]]
file = vim.fs.joinpath(base_dir, relative_file)
end
local path = dirman_utils.expand_pathlib(file, true):resolve()

vim.loop.fs_open(vim.fn.expand(file) --[[@as string]], "w", 438, function(err, fd)
vim.loop.fs_open(tostring(path), "w", 438, function(err, fd)
assert(not err and fd, lib.lazy_string_concat("Failed to open file '", file, "' for tangling: ", err))

local write_content = table.concat(content, "\n")
Expand Down

0 comments on commit 752bc97

Please sign in to comment.