Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for usability #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
INSTALL_PATH=/usr/local

build:
mkdir -p $(INSTALL_PATH)/{bin,lib}
install gacme $(INSTALL_PATH)/bin
install parseGeminiResponse.awk $(INSTALL_PATH)/lib
install getMeta.awk $(INSTALL_PATH)/lib
sed -i 's|INSTALL_PATH|$(INSTALL_PATH)|' $(INSTALL_PATH)/bin/gacme
chmod +x $(INSTALL_PATH)/bin/gacme

install: build

uninstall:
rm $(INSTALL_PATH)/bin/gacme
rm $(INSTALL_PATH)/lib/parseGeminiResponse.awk
rm $(INSTALL_PATH)/lib/getMeta.awk
rm $(INSTALL_PATH)/lib/getMeta.awk
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ Finally, add the following to `$HOME/lib/plumbing`:
type is text
data matches 'gemini://([a-zA-Z0-9_\-.]+[a-zA-Z0-9_@\-]+)/?'
plumb to web
plumb start gacme $0'/' $1 '/'
plumb start gacme $0 $1 '/'

# gemini links without a path but with a port
type is text
data matches 'gemini://([a-zA-Z0-9_\-.]+[a-zA-Z0-9_@\-]+):([0-9]+)/?'
plumb to web
plumb start gacme $0'/' $1 '/' $2
plumb start gacme $0 $1 '/' $2

# gemini links with a path but no port
type is text
Expand Down
44 changes: 30 additions & 14 deletions gacme
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

. $PLAN9/lib/acme.rc
fullResponse=`{mktemp}
binarydir=`{mktemp -d}

GACME_WIDTH=70

Expand All @@ -28,29 +29,44 @@ fn get {
fn parseResponse {
head=`{sed 1q $fullResponse}
code=$head(1)
if (~ $"code 20) {
type=`{echo $"head | ssam -n 'x/[a-z]+\/[a-z]+/p'}
switch ($"code) {
case 20
{
type=`{echo $"head | sed -E 's|[0-9]+\s([a-z]+/[a-z]+).*|\1|'}

if (~ $type text/gemini) {
tail +2l $fullResponse | awk -v `{printf 'hostname=%s' $1} \
-v `{printf 'path=%s' $2} \
-v `{printf 'width=%s' $GACME_WIDTH} \
-f /usr/local/lib/parseGeminiResponse.awk
switch($type) {
case text/gemini
{
cat $fullResponse | tail +2l \
| awk -v `{printf 'hostname=%s' $1} \
-v `{printf 'path=%s' $2} \
-v `{printf 'width=%s' $GACME_WIDTH} \
-f INSTALL_PATH/lib/parseGeminiResponse.awk \
| fold -s
}
case text/plain
tail +2l $fullResponse
case *
{
file=$binarydir/`{basename $2}
tail +2l $fullResponse > $file
plumb $file > /dev/null
echo $type saved in $file
}
}
}
if (~ $type text/plain) {
tail +2l $fullResponse
}
}
if not {
case 31
echo $head | sed -E 's|31 (gemini://'$1')?(.*)|Redirect: go to gemini://'$1'\2|'
case *
cat $fullResponse
}
}

if (~ $#* 4) {
echo $1 | get $2 $4 | tr -d \15 > $fullResponse
echo $1 | get $2 $4 > $fullResponse
}
if not {
echo $1 | get $2 1965 | tr -d \15 > $fullResponse
echo $1 | get $2 1965 > $fullResponse
}

newwindow
Expand Down
71 changes: 8 additions & 63 deletions parseGeminiResponse.awk
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@

BEGIN {
FS = " "
mono = 0
}

function pathForRelativeLink() {
str = ""
if (path == "") {
return "/"
if (path == "" || path == "/") {
return ""
} else {
len = split(path, parts, "/")
for (i = 1; i < len; i++) {
Expand All @@ -34,78 +33,24 @@ function pathForRelativeLink() {

# trying to turn any partial link into a fully qualified gemini link
function parseLink(link) {
if (link ~ /(https?|gemini|gopher|mailto|finger):\/\/.*/) {
if (link ~ /(https?|gemini|gopher|finger):\/\/.*/ || link ~ /mailto:/) {
return link
} else if (link ~ /^\/\/.*/) {
return "gemini:" link
} else if (index(link, "/") == 1) {
return "gemini://" hostname link
} else if (index(link, "/") > 1 || !index(link, "/")) {
return "gemini://" hostname pathForRelativeLink() link
return "gemini://" hostname "/" pathForRelativeLink() link
} else { # generic catch-all for me to find any bugs that arise
return link
}
}

function printFormatedLine(line, pre) {
if (pre == "") {
formatedLine = line[1]
} else {
formatedLine = pre
}

for (i = 2; i <= length(line); i++) {
word = line[i]
if (length(word) + length(formatedLine) > width) {
printf("%s\n", formatedLine)
formatedLine = word
} else {
formatedLine = sprintf("%s %s", formatedLine, word)
}
}
print(formatedLine)

}

/^=>/ {
str = "=>"
if (mono) {
print
} else {
if ($1 == str) {
$1 = ""
link = parseLink($2)
$2 = ""
} else {
sub(/=>/, "")
link = parseLink($1)
$1 = ""
}
printf("\n⇒%s\n⇒\t%s\n", $0, link)
}
link = parseLink($2)
sub($2, "")
printf $0 "\n" link "\n\n"
next
}

/^\* / {
if (!mono) {
sub(/\*/, "•")
split($0, words, " ")
printFormatedLine(words, "•")
} else print
next
}

/^```/ {
(mono == 0) ? mono = 1 : mono = 0
print("```")
next
}

{
if (mono) {
print
} else {
split($0, words, " ")
printFormatedLine(words, "")
}
}
{print}