about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-12-15 17:14:02 +0100
committerbptato <nincsnevem662@gmail.com>2024-12-15 17:22:13 +0100
commit6548f88e7dd0f6dcd43ada4f6581510f97c95bf7 (patch)
tree1b9c56e38c3d5be5e258e99175b42963beb070eb
parent8a586beaa26fa7ad4dbc4573e655927f8f32e158 (diff)
downloadchawan-6548f88e7dd0f6dcd43ada4f6581510f97c95bf7.tar.gz
chabookmark: fix new item placement, better error handling
it should put it after the old items, not before them.
-rwxr-xr-xadapter/protocol/chabookmark43
1 files changed, 25 insertions, 18 deletions
diff --git a/adapter/protocol/chabookmark b/adapter/protocol/chabookmark
index 8bfe59cf..9b2517c8 100755
--- a/adapter/protocol/chabookmark
+++ b/adapter/protocol/chabookmark
@@ -146,28 +146,35 @@ new() {
 	if ! test -f "$bookmark_file"; then init || exit 1; fi
 	if test -n "$new_section"
 	then	section=$new_section
-		safe_printf '\n## %s\n' "$new_section" >> "$bookmark_file"
+		safe_printf '\n## %s\n' "$new_section" >>"$bookmark_file"
+	elif test -z "$section"
+	then	die "InternalError section not specified"
 	fi
 	new_tmp="$bookmark_file~"
 	new_found=0
 	safe_rm "$new_tmp"
 	while read -r line
-	do	safe_println "$line" >> "$new_tmp"
+	do	safe_println "$line" >>"$new_tmp"
 		if test "$new_found" = 0 && test "$line" = "## $section"
-		then	read -r line
-			if test -n "$line"
-			then	safe_println "$line" >> "$new_tmp"
-				while read -r line && test -n "$line"
-				do	safe_println "$line" >> "$new_tmp"
-				done
-			else	safe_println '' >> "$new_tmp"
-				line=
+		then	if read -r line && test -n "$line"
+			then	die "InternalError malformed section $section"
+			fi
+			safe_println "$line" >>"$new_tmp"
+			while read -r line
+			do	case $line in
+				'#'*)	die "InternalError malformed section $section";;
+				'')	new_found=1; break;;
+				*)	safe_println "$line" >>"$new_tmp";;
+				esac
+			done
+			safe_println "* [$title]($url)" >>"$new_tmp"
+			if test "$new_found" = 1
+			then	safe_println '' >>"$new_tmp"
+			else	new_found=1
 			fi
-			safe_println "* [$title]($url)" >> "$new_tmp"
-			new_found=1
 		fi
 	done <"$bookmark_file"
-	mv "$new_tmp" "$bookmark_file" || die InternalError "could not move temp file"
+	mv "$new_tmp" "$bookmark_file" || die "InternalError could not move temp file"
 	view
 }
 
@@ -181,7 +188,7 @@ a.heading { display: none }
 # Bookmarks'
 
 init() {
-	safe_println "$init_string" >> "$bookmark_file"
+	safe_println "$init_string" >>"$bookmark_file"
 }
 
 import_w3m() {
@@ -191,7 +198,7 @@ import_w3m() {
 	fi
 	import_w3m_tmp="$bookmark_file~"
 	safe_rm "$import_w3m_tmp"
-	safe_println "$init_string" >> "$import_w3m_tmp"
+	safe_println "$init_string" >>"$import_w3m_tmp"
 	#TODO this isn't quite right. Specifically, if a URL has unmatched
 	# parentheses/brackets in it, then you get a malformed markdown file.
 	# But also, the user can just edit it manually, it's not that hard.
@@ -200,8 +207,8 @@ import_w3m() {
 		-e 's@</?ul>|<body>@@g' \
 		-e '/^<!--|^<h1>|^<\/?body>|^<\/?html>|^<meta charset|^<\/?head>/d' \
 		-e 's@<li><a href="([^"]+)">([^<]+)</a>@* [\2](\1)@g' \
-		"$w3m_path" >> "$import_w3m_tmp" || die "InternalError error importing file"
-	mv "$import_w3m_tmp" "$bookmark_file" || die InternalError "could not move temp file"
+		"$w3m_path" >>"$import_w3m_tmp" || die "InternalError error importing file"
+	mv "$import_w3m_tmp" "$bookmark_file" || die "InternalError could not move temp file"
 	view
 }
 
@@ -234,5 +241,5 @@ view)	view;;
 new)	post_only && new;;
 init)	post_only && init && view;;
 w3m)	post_only && import_w3m && view;;
-*)	die InvalidURL "unknown action $action";;
+*)	die "InvalidURL unknown action $action";;
 esac