65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package vistas
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/http"
|
|
"os/exec"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func Buscador(c *gin.Context) {
|
|
busqueda := c.PostForm("busqueda")
|
|
//Sanitize "busqueda"
|
|
busqueda = regexp.MustCompile(`[^a-zA-Z0-9ñÑáéíóú\- ]+`).ReplaceAllString(busqueda, "")
|
|
|
|
cmd := exec.Command("rg", "-l", busqueda, "./letras")
|
|
var out bytes.Buffer
|
|
cmd.Stdout = &out
|
|
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
resultado := "Error buscando :("
|
|
fmt.Println(resultado)
|
|
}
|
|
|
|
res := ""
|
|
atopadas := strings.Split(out.String(), "\n")
|
|
if len(atopadas) > 0 {
|
|
res = res + "<ul>"
|
|
for _, linea := range atopadas {
|
|
// fmt.Println(linea)
|
|
//coller slug
|
|
nome := strings.Replace(linea, "./letras/", "", -1) //quita path
|
|
slug := strings.Replace(nome, ".md", "", -1) //quita exten
|
|
|
|
//coller titulo
|
|
grep := exec.Command("grep", "title", linea)
|
|
var salida bytes.Buffer
|
|
grep. Stdout = &salida
|
|
err := grep.Run()
|
|
if err != nil {
|
|
resultado := "Error buscando :("
|
|
fmt.Println(resultado)
|
|
}
|
|
partes := strings.Split(salida.String(), "\"")
|
|
titulo := ""
|
|
if len(partes) > 1 {
|
|
titulo = partes[1]
|
|
}
|
|
|
|
// montar salida
|
|
if titulo != "" && slug != "" {
|
|
res = res + "<li><a href=# hx-post='/letra' hx-vals='{\"slug\": \""+slug+"\"}' hx-trigger='click' hx-target='#search-results'>"+titulo+"</a></li>"
|
|
}
|
|
}
|
|
res = res + "</ul>"
|
|
}
|
|
|
|
// fmt.Println(res)
|
|
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(res))
|
|
}
|