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

# search

## enabling in werc

In your `/_werc/config` add the line `conf_enable_searching`

## tank

tank is a small Go program that uses ripgrep, to compile run

`go build -o tank tank.go`

Place in /lib/plan9/bin or your bin dir. that werc sources

<hr>

See, <https://github.com/risingThumb/search/> for a shell-only approach

app.rc

fn conf_enable_searching {
    enable_searching=yes
    conf_enable_app search
        ll_add handlers_bar_left tpl_handler apps/search/sbar.tpl
}

sbar.tpl

<div>
	<form action="" method="POST" enctype="multipart/form-data">
	<input type="search" name="searching_query" placeholder="Search..." required><br>
	<input type="submit" name="searching_preview" value="Search" style="display:none">
	<input type="checkbox" id="search_this_directory" name="search_app_search_directory" value="search_app_search_directory">
	<label for="search_this_directory">Include filenames?</label>
	</form>
</div>

% if(! ~ $#post_arg_search_app_search_directory 0){
%	filenames=`{walk sites/$SERVER_NAME | grep -v -e '_werc' -e '.meta' | grep -i $post_arg_searching_query}
	<p>Filenames:
%		for(match in $filenames){
%			href_path=`{echo $match | sed -e 's/.md//g' -e 's|sites/'$SERVER_NAME'||g'}
			<a href="%($href_path%)">%($href_path%)</a>
%		}
%		if(~ $#filenames 0 || ){
			<p>No filename matches for "<i>%($"post_arg_searching_query%)</i>"</p>
%		}
% }

% if (! ~ $"post_arg_searching_preview) {
%	tank $post_arg_searching_query 
% }


tank.go

package main

import (
	"bufio"
	"fmt"
	"html"
	"os"
	"os/exec"
	"strings"
)

const (
	maxLength = 35
	baseDir   = "/var/www/werc/sites/c.bauherren.ovh"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("Provide one search term")
		os.Exit(1)
	}

	searchTerm := os.Args[1]
	if len(searchTerm) > maxLength {
		fmt.Printf("Search term is too long. Maximum allowed length is %d characters.\n", maxLength)
		os.Exit(1)
	}

	fmt.Printf("<link rel=stylesheet href=/_werc/pub/search-results.css><div id=\"search-results\">\n")
	fmt.Printf("<h3>Search results for: \"%s\"</h3>\n", html.EscapeString(searchTerm))

	cmd := exec.Command("rg", "-i",  "--no-heading", "--with-filename", "--line-number", searchTerm, "--glob", "*.md", baseDir)
	output, err := cmd.Output()
	if err != nil {
		fmt.Println("No results")
		os.Exit(1)
	}

	scanner := bufio.NewScanner(strings.NewReader(string(output)))
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.SplitN(line, ":", 3)
		if len(parts) < 3 {
			continue
		}

		file := strings.TrimPrefix(parts[0], baseDir)
		file = strings.ReplaceAll(file, ".md", "")
		lineNumber := parts[1]
		content := parts[2]

		escapedContent := html.EscapeString(content)
		formattedContent := strings.ReplaceAll(escapedContent, searchTerm, "<b>"+searchTerm+"</b>")

		fmt.Printf("<div class=\"search-item\">\n")
		fmt.Printf("<a href=\"%s\">%s</a> (Line %s)<br>\n", file, file, lineNumber)
		fmt.Printf("<p>\"%s\"</p>\n", formattedContent)
		fmt.Printf("</div>\n")
	}

	fmt.Printf("</div>\n")

	if err := scanner.Err(); err != nil {
		fmt.Println("Error reading output:", err)
		os.Exit(1)
	}
}