Files

162 lines
11 KiB
Markdown
Raw Permalink Normal View History

2023-03-31 16:21:19 +03:00
# Bopher Tools: a set of simple Bash scripts to not just browse Gopher but also create content for it
2023-04-03 17:34:10 +03:00
Bopher Tools is a small collection of simple shell scripts that:
2023-04-03 19:14:01 +03:00
- are written in pure Bash with no external dependencies (unless explicitly stated otherwise);
2023-04-03 17:34:10 +03:00
- are totally independent from each other (but can be combined in the true spirit of Unix philosophy);
- only accept input from stdin and only output their results into stdout;
- aim to ease your life when it comes to creating and publishing your own content in Gopherspace.
It is recommended to distribute these scripts along with the main Bopher-NG client, although everyone is free to redistribute them in any other form. Just like the main Bopher-NG script, all these tools are released into public domain. Their documentation is added to this page in the chronological order they were created.
Without further ado, let's begin!
## `gopherinfo.sh`
This tool converts every line of the standard input into a valid CRLF-terminated Gophermap line of the `i` type that obeys RFC1436. It accepts three **optional** parameters: the number of leading spaces (0 by default), the number of trailing spaces (0 by default) and the placeholder (';' by default) to put into the selector and host fields unused in the `i` type (and the port field is always set to 0). It also pads every input line with the amount of spaces required to make the line length the same as the longest one in the input. All this is done to make your output Gophermap look nice when viewed in plaintext as well.
2023-03-31 16:21:19 +03:00
Note that the tool **does not** output the trailing dot to end the Gophermap, because its output can be used as a part of another Gophermap. To end the map manually according to the RFC1436, just invoke `printf '.\r\n'` afterwards redirecting to the same output.
2023-03-31 16:21:19 +03:00
Example of using `gopherinfo.sh` in combination with FIGlet, using 4 leading spaces, 0 trailing spaces and `|` as the placeholder (CR and Tab characters in the output are not shown):
```
$ figlet 'Bash it' | bash gopherinfo.sh 4 0 '|'
i ____ _ _ _ | | 0
i | __ ) __ _ ___| |__ (_) |_ | | 0
i | _ \ / _` / __| '_ \ | | __| | | 0
i | |_) | (_| \__ \ | | | | | |_ | | 0
i |____/ \__,_|___/_| |_| |_|\__| | | 0
i | | 0
```
A non-trivial task would be using this output in some Web-based services like Happy Net Box. You have to paste it in a format that the browser's text input element wouldn't mess with. To do this, you need the following steps:
1. Pipe the `gopherinfo.sh` output into the autoquoter like this: `| ( read -rsd '' x; printf '%q\n' "$x" )`
2. Copy everything except the first dollar sign of the output to your clipboard.
3. In your Web browser, open a developer console (F12), type `console.log()` and put the cursor between the brackets.
4. Paste your escaped string and hit Enter. If everything went correctly, the output should not be mangled.
5. Copy your `console.log` output again (make sure to fully copy the last line too!) and paste it in your Web service's posting textarea.
2023-03-31 16:21:19 +03:00
This hack also appies to any Gophermaps and not just the fragments generated with this tool.
2023-03-31 22:08:41 +03:00
Or, if you happen to have a working clipboard, you can just pipe the tools' output to `| xsel -bi` (on Linux) or `| pbcopy` (on Mac) and try pasting it directly. In all probability, it might work, but no one can guarantee.
2023-03-31 16:21:19 +03:00
## `phlow.sh`
This tool processes every line of text from the standard input to fit exactly the given amount of characters, optionally adding some leading and/or trailing whitespaces **after** the reflow is done. It also replaces all LF line endings with CRLF in the output to achieve maximum compatibility with legacy clients.
As `phlow.sh` does not know anything about hyphenation rules and doesn't do any heuristics, it just breaks at whatever whitespace is the closest to the page's right edge.
2023-03-31 19:17:11 +03:00
Example - fitting the previous paragraph into 30-character width and prepending every line with 5 spaces (CR characters in the output are not shown):
2023-03-31 16:21:19 +03:00
```
$ echo 'This tool processes every line of text from the standard input to fit exactly the given amount of characters, optionally adding some leading and/or trailing whitespaces **after** the reflow is done. It also replaces all LF line endings with CRLF in the output to achieve maximum compatibility with legacy clients.' | bash phlow.sh 30 5
This tool processes every
line of text from the
standard input to fit
exactly the given amount of
characters, optionally
adding some leading and/or
trailing whitespaces
**after** the reflow is
done. It also replaces all
LF line endings with CRLF in
the output to achieve
maximum compatibility with
legacy clients.
```
2023-03-31 19:17:11 +03:00
## `gmi2map.sh`
2023-03-31 16:21:19 +03:00
2023-03-31 19:17:11 +03:00
This tool converts Gemtext documents into browsable Gophermaps. It basically combines the functionality of both previous tools and adds link processing and triple-backtick removal (because in Gopherspace, all text is supposed to be preformatted) on top of them. Unlike `gopherinfo.sh` though, it doesn't care about the maximum length of the source (if you need to, the reflow logic is there), but it **is** expecting the entire document from the standard input, and as such, **does** add the trailing dot at the end of the generated Gophermap. That's why, for the beginners, this sole script might actually be everything you need to get you started with Gopher publishing.
Note that by default, reflow logic in `gmi2map.sh` is optional and you have to specify the page width yourself if you need it. Actually, full usage syntax looks like this:
```
cat [file] | gmi2map.sh [page_width] [leading_spaces] [trailing_spaces] [placeholder_char]
```
2023-04-03 21:37:12 +03:00
I.e. it looks like the `phlow.sh` and `gopherinfo.sh` options combined, but the spaces part only applies to the reflow, which is also off by default. Also, it doesn't align the whitespaces after the infolines unless there is non-zero amount of trailing spaces.
2023-03-31 19:17:11 +03:00
Example - imagine we have this rudimentary Gemtext document in the `example.gmi` file:
```
I write short Gemtext here.
I write a bit longer Gemtext here to showcase reflow capabilities of Bopher Tools' gmi2map.sh script, because it really is cool.
=> gopher://happynetbox.com:79/1luxferre Anyway, why not publish it on Gopher?
=> https://chronovir.us Or read my blog on Web...
See ya!
```
2023-04-03 21:37:12 +03:00
When running `cat example.gmi | bash gmi2map.sh 67 0 1`, this file translates to the following Gophermap:
2023-03-31 19:17:11 +03:00
```
$ cat example.gmi | bash gmi2map.sh 67
2023-04-03 21:37:12 +03:00
iI write short Gemtext here. ; ; 0
i ; ; 0
iI write a bit longer Gemtext here to showcase reflow capabilities ; ; 0
iof Bopher Tools' gmi2map.sh script, because it really is cool. ; ; 0
i ; ; 0
2023-03-31 19:17:11 +03:00
1Anyway, why not publish it on Gopher? luxferre happynetbox.com 79
hOr read my blog on Web... URL:https://chronovir.us ; 0
2023-04-03 21:37:12 +03:00
i ; ; 0
iSee ya! ; ; 0
2023-03-31 19:17:11 +03:00
.
```
Note that this tool hasn't been tested for all possible edge cases yet, so I recommend to always verify the generated maps yourself. But it definitely makes their creation a lot easier than doing it by hand.
2023-04-03 17:25:58 +03:00
## `gmi2txt.sh`
2023-04-03 17:34:10 +03:00
This tool converts Gemtexts into CRLF-terminated and preformatted plaintext files ready to be served on Gopher. It has the same parameters as `gmi2map.sh` sans the placeholder character. Link lines are converted to the `[description]: [url]` format and, unlike `gmi2map.sh`, also are subject to reflows like everything else.
2023-04-03 17:25:58 +03:00
Example - imagine we have this rudimentary Gemtext document in the `example.gmi` file:
```
I write short Gemtext here.
I write a bit longer Gemtext here to showcase reflow capabilities of Bopher Tools' gmi2txt.sh script, because it really is cool.
=> gopher://happynetbox.com:79/1luxferre Visit my Gopher homepage
=> https://chronovir.us Or read my blog on Web
See ya!
```
When running `cat example.gmi | bash gmi2txt.sh 67 5`, this file translates to the following plaintext:
```
I write short Gemtext here.
I write a bit longer Gemtext here to showcase reflow capabilities
of Bopher Tools' gmi2txt.sh script, because it really is cool.
Visit my Gopher homepage: gopher://happynetbox.com:79/1luxferre
Or read my blog on Web: https://chronovir.us
See ya!
```
2023-04-03 19:14:01 +03:00
## `list2map.sh`
This tool, which probably is the smallest but the most sophisticated one in the entire collection, accepts the list of files and directories (possibly generated with `find` command) and generates, line by line, a portion of Gophermap based on their type (for this, it requires the external `file` command dependency), root directory name and hostname/port number optionally passed to the script on invocation. The tool automatically applies the following rules:
- only the basename part of the file/directory is used as the entry name/description;
- if the file is a directory (MIME type `inode/directory`), the generated selector will actually refer to the `index.map` file in that directory and the entry type will be 1;
- files (or directories) named `index.map` are not themselves included in the output;
- if the file's MIME type starts with `text`, the entry type will be set to 0, otherwise it will be set to 9.
Note that the tool itself doesn't do any filtering on input names or how they are presented. All filtering, if necessary, should be done on the `find` invocation step.
Example - build a Gophermap fragment from this repo's starting directory, excluding all dotfiles, using `.` as root and assuming we'll serve this map from `example.com:777`:
```
$ find . ! -path '.' ! -path '*/.*' | bash tools/list2map.sh . example.com 777
0COPYING /COPYING example.com 777
1tools /tools/index.map example.com 777
0gopherinfo.sh /tools/gopherinfo.sh example.com 777
0phlow.sh /tools/phlow.sh example.com 777
0gmi2map.sh /tools/gmi2map.sh example.com 777
0gmi2txt.sh /tools/gmi2txt.sh example.com 777
0README-tools.md /tools/README-tools.md example.com 777
0list2map.sh /tools/list2map.sh example.com 777
0bopher-ng.sh /bopher-ng.sh example.com 777
0README.md /README.md example.com 777
```