From 752bc97a7a0557578cfbf1fdeca140a800e62a27 Mon Sep 17 00:00:00 2001 From: Ben Lubas Date: Sat, 16 Nov 2024 22:57:46 -0500 Subject: [PATCH] fix: weird tangle file path logic --- .../modules/core/dirman/utils/module.lua | 9 ++---- lua/neorg/modules/core/tangle/module.lua | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lua/neorg/modules/core/dirman/utils/module.lua b/lua/neorg/modules/core/dirman/utils/module.lua index 10b33e6ab..aadf8b4f9 100644 --- a/lua/neorg/modules/core/dirman/utils/module.lua +++ b/lua/neorg/modules/core/dirman/utils/module.lua @@ -19,12 +19,9 @@ local module = neorg.modules.create("core.dirman.utils") module.public = { ---Resolve `$/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 diff --git a/lua/neorg/modules/core/tangle/module.lua b/lua/neorg/modules/core/tangle/module.lua index a1db7af20..b2e925dd2 100644 --- a/lua/neorg/modules/core/tangle/module.lua +++ b/lua/neorg/modules/core/tangle/module.lua @@ -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 ` tag above the code block you'd wish to export, where is relative to the @@ -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 @@ -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 @@ -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 = { @@ -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")