clutteredmind | special thanks |

bauherren.ovh

search

search, extensive searchbar functionality, can grep out strings in files and their filename

Mirrored from GitHub

git clone https://github.com/christc4/search.git

Jump to: README.md app.rc sbar.tpl tank.go


README.md

1    	# search
2    	
3    	## enabling in werc
4    	
5    	In your `/_werc/config` add the line `conf_enable_searching`
6    	
7    	## tank
8    	
9    	tank is a small Go program that uses ripgrep, to compile run
10   	
11   	`go build -o tank tank.go`
12   	
13   	Place in /lib/plan9/bin or your bin dir. that werc sources
14   	
15   	<hr>
16   	
17   	See, <https://github.com/risingThumb/search/> for a shell-only approach

app.rc

1    	fn conf_enable_searching {
2    	    enable_searching=yes
3    	    conf_enable_app search
4    	        ll_add handlers_bar_left tpl_handler apps/search/sbar.tpl
5    	}
6    	

sbar.tpl

1    	<div>
2    		<form action="" method="POST" enctype="multipart/form-data">
3    		<input type="search" name="searching_query" placeholder="Search..." required><br>
4    		<input type="submit" name="searching_preview" value="Search" style="display:none">
5    		<input type="checkbox" id="search_this_directory" name="search_app_search_directory" value="search_app_search_directory">
6    		<label for="search_this_directory">Include filenames?</label>
7    		</form>
8    	</div>
9    	
10   	% if(! ~ $#post_arg_search_app_search_directory 0){
11   	%	filenames=`{walk sites/$SERVER_NAME | grep -v -e '_werc' -e '.meta' | grep -i $post_arg_searching_query}
12   		<p>Filenames:
13   	%		for(match in $filenames){
14   	%			href_path=`{echo $match | sed -e 's/.md//g' -e 's|sites/'$SERVER_NAME'||g'}
15   				<a href="%($href_path%)">%($href_path%)</a>
16   	%		}
17   	%		if(~ $#filenames 0 || ){
18   				<p>No filename matches for "<i>%($"post_arg_searching_query%)</i>"</p>
19   	%		}
20   	% }
21   	
22   	% if (! ~ $"post_arg_searching_preview) {
23   	%	tank $post_arg_searching_query 
24   	% }
25   	
26   	

tank.go

1    	package main
2    	
3    	import (
4    		"bufio"
5    		"fmt"
6    		"html"
7    		"os"
8    		"os/exec"
9    		"strings"
10   	)
11   	
12   	const (
13   		maxLength = 35
14   		baseDir   = "/var/www/werc/sites/c.bauherren.ovh"
15   	)
16   	
17   	func main() {
18   		if len(os.Args) < 2 {
19   			fmt.Println("Provide one search term")
20   			os.Exit(1)
21   		}
22   	
23   		searchTerm := os.Args[1]
24   		if len(searchTerm) > maxLength {
25   			fmt.Printf("Search term is too long. Maximum allowed length is %d characters.\n", maxLength)
26   			os.Exit(1)
27   		}
28   	
29   		fmt.Printf("<link rel=stylesheet href=/_werc/pub/search-results.css><div id=\"search-results\">\n")
30   		fmt.Printf("<h3>Search results for: \"%s\"</h3>\n", html.EscapeString(searchTerm))
31   	
32   		cmd := exec.Command("rg", "-i",  "--no-heading", "--with-filename", "--line-number", searchTerm, "--glob", "*.md", baseDir)
33   		output, err := cmd.Output()
34   		if err != nil {
35   			fmt.Println("No results")
36   			os.Exit(1)
37   		}
38   	
39   		scanner := bufio.NewScanner(strings.NewReader(string(output)))
40   		for scanner.Scan() {
41   			line := scanner.Text()
42   			parts := strings.SplitN(line, ":", 3)
43   			if len(parts) < 3 {
44   				continue
45   			}
46   	
47   			file := strings.TrimPrefix(parts[0], baseDir)
48   			file = strings.ReplaceAll(file, ".md", "")
49   			lineNumber := parts[1]
50   			content := parts[2]
51   	
52   			escapedContent := html.EscapeString(content)
53   			formattedContent := strings.ReplaceAll(escapedContent, searchTerm, "<b>"+searchTerm+"</b>")
54   	
55   			fmt.Printf("<div class=\"search-item\">\n")
56   			fmt.Printf("<a href=\"%s\">%s</a> (Line %s)<br>\n", file, file, lineNumber)
57   			fmt.Printf("<p>\"%s\"</p>\n", formattedContent)
58   			fmt.Printf("</div>\n")
59   		}
60   	
61   		fmt.Printf("</div>\n")
62   	
63   		if err := scanner.Err(); err != nil {
64   			fmt.Println("Error reading output:", err)
65   			os.Exit(1)
66   		}
67   	}
68