From 356c6130389c230e42827fac184ed30b3387bcb4 Mon Sep 17 00:00:00 2001 From: MatrixFurry Date: Tue, 4 Aug 2026 23:00:45 -0500 Subject: [PATCH] new script: rawreplace --- README.md | 24 ++++++++-- meta/{generate-readme.nu => update-readme.nu} | 48 +++++++------------ scripts/rawreplace.nu | 42 ++++++++++++++++ 3 files changed, 81 insertions(+), 33 deletions(-) rename meta/{generate-readme.nu => update-readme.nu} (66%) create mode 100755 scripts/rawreplace.nu diff --git a/README.md b/README.md index 2626d63..0490711 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ - - # MatrixFurry's Scripts Small (but incredibly useful!) scripts and utilities that don't need their own repo. @@ -10,6 +8,8 @@ See `--help` for detailed usage info & examples! # Featured + + ## rawrsync Easily sync or deploy projects using `rsync`. Intended to somewhat replicate CLion's Deployment feature for use with other IDEs. @@ -26,6 +26,16 @@ address = "vr-desktop.tailnet-name.ts.net" path = "/home/username/vr/project" ``` +## rawreplace +Replace content between elements in a markdown file + +Allows you to directly edit the static parts of your markdown files without +using templates, while also being able to update the content that is +auto-generated by your CI scripts. + +This is useful, for example, to keep package lists and descriptions +up to date, and is used in this repo (see meta/update-readme.nu)! + ## atproto-app-passwords Create an app password for your ATProto account. @@ -47,9 +57,12 @@ Get an RSS feed for the specified YouTube channel. ## far Find and replace text from all files in a directory, using the `str replace` syntax + # Other Scripts + + ## abs-nu-exec Useful for executing nushell scripts in GNOME keyboard shortcuts without adding Homebrew PATH to your shell environment @@ -118,8 +131,13 @@ Caveats to this approach: ## toggle-extension Provides an easy way to toggle a GNOME extension. Useful for setting up keyboard shortcuts. + -# Deprecated +## Deprecated + + ## podman-remote-tcp Create a Podman remote TCP connection to a machine over SSH. This is insecure, be careful. + + diff --git a/meta/generate-readme.nu b/meta/update-readme.nu similarity index 66% rename from meta/generate-readme.nu rename to meta/update-readme.nu index 893eacb..d4eddd4 100755 --- a/meta/generate-readme.nu +++ b/meta/update-readme.nu @@ -2,14 +2,11 @@ # SPDX-License-Identifier: AGPL-3.0-only # Copyright (c) 2026 MatrixFurry -# -> Editable <- # -const h1 = "# MatrixFurry's Scripts -Small (but incredibly useful!) scripts and utilities that don't need their own repo. - -See `--help` for detailed usage info & examples!" +use ../scripts/rawreplace.nu const featured_names = [ rawrsync + rawreplace atproto-app-passwords update-gitignore ln-bin @@ -20,18 +17,12 @@ const featured_names = [ # -> Start <- # const repo = path self .. +const readme = $repo | path join README.md # Generate a readme using the `main` documentation from each script export def main [ --debug (-d) # Print the README instead of saving it ] { - let md_comments = [ - "" - $"" - "" - "\n" - ] | str join "\n" - let paths = ls ($repo + "/scripts") | where type == file | get name @@ -49,26 +40,21 @@ export def main [ let rank = $featured_names | enumerate | reduce --fold {} {|it, acc| $acc | insert $it.item $it.index} let featured = $not_deprecated | where $it.name in $featured_names | insert rank {|row| $rank | get $row.name} | sort-by rank | reject rank - [ - $md_comments - $h1 - "\n# Featured" - (generate-section $featured) - "\n# Other Scripts" - (generate-section $unfeatured) - "\n# Deprecated" - (generate-section $deprecated) - ] - | str join "\n" - | if $debug { - print $in - } else { - $in | save -fp ($repo | path join README.md) - } + open -r $readme + | update-section featured $featured + | update-section other $unfeatured + | update-section deprecated $deprecated + | collect + | save -f $readme } -def generate-section [scripts: table] { - $scripts +def update-section [ + tag: string + scripts: table +]: string -> string { + let md = $in + + let section = $scripts | each {|e| if ($e.description | is-empty) { log warning $"No documentation provided for `($e.name)`, it will be excluded from the readme." @@ -84,4 +70,6 @@ def generate-section [scripts: table] { return $h2 } | str join + + $md | rawreplace $tag ("\n" ++ $section ++ "\n") } diff --git a/scripts/rawreplace.nu b/scripts/rawreplace.nu new file mode 100755 index 0000000..e7f98a4 --- /dev/null +++ b/scripts/rawreplace.nu @@ -0,0 +1,42 @@ +#!/usr/bin/env nu +# SPDX-License-Identifier: AGPL-3.0-only +# Copyright (c) 2026 MatrixFurry + +# Replace content between elements in a markdown file +# +# Allows you to directly edit the static parts of your markdown files without +# using templates, while also being able to update the content that is +# auto-generated by your CI scripts. +# +# This is useful, for example, to keep package lists and descriptions +# up to date, and is used in this repo (see meta/update-readme.nu)! +export def main [ + tag: string + body: string + path?: path +]: [ + nothing -> nothing + string -> string +] { + let pipeline = $in != null + let input = $in + + if not $pipeline and not ($path | path exists) { + error make -u { + msg: $"Could not open the specified file: `($path)`" + } + } + + if $pipeline { + $input + } else { + open $path --raw + } + | str replace --regex $"\(?s\)\(?<=\).*?\(?=\)" $body + | if $pipeline { + $in + } else { + collect + | save -f $path + } +} -- 2.51.2