Formatting

This commit is contained in:
Sarah Jamie Lewis 2019-11-11 19:07:25 -08:00
parent 37bebe271e
commit 6543f89843
4 changed files with 51 additions and 53 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
tmp.dot
tmp.png
tmp.svg
.idea/

View File

@ -14,9 +14,9 @@ import (
"strings" "strings"
) )
func ExtractPDF(extract string) (string,string,string,*bibtex.BibEntry) { func ExtractPDF(extract string) (string, string, string, *bibtex.BibEntry) {
log.Infof("Extracting..." + extract) log.Infof("Extracting..." + extract)
parts := strings.Split(extract,";") parts := strings.Split(extract, ";")
if len(parts) >= 3 { if len(parts) >= 3 {
args := strings.Split(parts[1], ":") args := strings.Split(parts[1], ":")
@ -36,22 +36,22 @@ func ExtractPDF(extract string) (string,string,string,*bibtex.BibEntry) {
bt := FindReference(src) bt := FindReference(src)
title := "From " + src title := "From " + src
if bt != nil { if bt != nil {
title = bt.Fields["title"].String() + " by " + bt.Fields["author"].String() + " (" + bt.Fields["year"] .String()+ ")" title = bt.Fields["title"].String() + " by " + bt.Fields["author"].String() + " (" + bt.Fields["year"].String() + ")"
} }
return title,link, "<img class=\"source\" id=\"" + link + "\" src=\"data:image/png;base64, " + base64.StdEncoding.EncodeToString(data) + "\"/>",bt return title, link, "<img class=\"source\" id=\"" + link + "\" src=\"data:image/png;base64, " + base64.StdEncoding.EncodeToString(data) + "\"/>", bt
} }
return "","","",nil return "", "", "", nil
} }
func FindReference(url string) *bibtex.BibEntry{ func FindReference(url string) *bibtex.BibEntry {
file,err := os.Open("./data/references.kdb") file, err := os.Open("./data/references.kdb")
if err == nil { if err == nil {
bt,err := bibtex.Parse(file) bt, err := bibtex.Parse(file)
if err != nil { if err != nil {
log.Errorf("%v", err) log.Errorf("%v", err)
} }
for _,e := range bt.Entries { for _, e := range bt.Entries {
if e.Fields["url"].String() == url { if e.Fields["url"].String() == url {
return e return e
} }

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"path" "path"
) )
func makeTorifiedClient() *http.Client { func makeTorifiedClient() *http.Client {
torDialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct) torDialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
if err != nil { if err != nil {
@ -29,12 +30,12 @@ func Fetch(url string, cacheid string) []byte {
log.Infof("Fetching..." + url) log.Infof("Fetching..." + url)
cachepath := path.Join("cache/", cacheid) cachepath := path.Join("cache/", cacheid)
if _, err := os.Stat(cachepath); !os.IsNotExist(err) { if _, err := os.Stat(cachepath); !os.IsNotExist(err) {
data,_ := ioutil.ReadFile(cachepath) data, _ := ioutil.ReadFile(cachepath)
return data return data
} }
client := makeTorifiedClient() client := makeTorifiedClient()
resp,err:= client.Get(url) resp, err := client.Get(url)
if err == nil { if err == nil {
defer resp.Body.Close() defer resp.Body.Close()
@ -45,5 +46,3 @@ func Fetch(url string, cacheid string) []byte {
log.Errorf("Error fetching: %v", err) log.Errorf("Error fetching: %v", err)
return []byte{} return []byte{}
} }

23
kdb.go
View File

@ -1,14 +1,13 @@
package main package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"git.openprivacy.ca/sarah/kdb/citing" "git.openprivacy.ca/sarah/kdb/citing"
"github.com/nickng/bibtex" "github.com/nickng/bibtex"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -76,15 +75,15 @@ func loadPage(title string) (*Page, error) {
if hasPage(word) { if hasPage(word) {
body = strings.ReplaceAll(body, word, fmt.Sprintf("<a href='/view/%v/'>%v</a>", normalize(word), normalize(word))) body = strings.ReplaceAll(body, word, fmt.Sprintf("<a href='/view/%v/'>%v</a>", normalize(word), normalize(word)))
} }
if strings.HasPrefix(word,"cite;") { if strings.HasPrefix(word, "cite;") {
log.Infof("Extracting Citation") log.Infof("Extracting Citation")
cite,link,img,rec := citing.ExtractPDF(word) cite, link, img, rec := citing.ExtractPDF(word)
if rec != nil { if rec != nil {
if references[rec.CiteName] == nil { if references[rec.CiteName] == nil {
references[rec.CiteName] = rec references[rec.CiteName] = rec
} }
} }
body = strings.ReplaceAll(body, word, fmt.Sprintf("<a title=\"%v\" href=\"javascript:showsource('%v')\">[X]</a>%v",cite,link,img)) body = strings.ReplaceAll(body, word, fmt.Sprintf("<a title=\"%v\" href=\"javascript:showsource('%v')\">[X]</a>%v", cite, link, img))
} }
if strings.HasPrefix(word, "http") { if strings.HasPrefix(word, "http") {
body = strings.ReplaceAll(body, word, fmt.Sprintf("<a onmouseover=\"javascript:showsource('%v')\" href='%v'></a>", word, word, word)) body = strings.ReplaceAll(body, word, fmt.Sprintf("<a onmouseover=\"javascript:showsource('%v')\" href='%v'></a>", word, word, word))
@ -96,7 +95,7 @@ func loadPage(title string) (*Page, error) {
} }
} }
return &Page{Title: title, Body: template.HTML(body), BodyOrig: string(bodyb), Concept: concept, References:references}, nil return &Page{Title: title, Body: template.HTML(body), BodyOrig: string(bodyb), Concept: concept, References: references}, nil
} }
func normalize(word string) string { func normalize(word string) string {
@ -124,7 +123,6 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "view", p) renderTemplate(w, "view", p)
} }
func saveHandler(w http.ResponseWriter, r *http.Request) { func saveHandler(w http.ResponseWriter, r *http.Request) {
title, err := getTitle(w, r) title, err := getTitle(w, r)
if err != nil { if err != nil {
@ -157,15 +155,15 @@ func get_links(name string) (links []string) {
func construct_graph(word string) string { func construct_graph(word string) string {
seen := make(map[string]bool) seen := make(map[string]bool)
graph := "digraph {\n ratio=\"compress\";size=\"8,4!\";splines=true;\""+word+"\";\n" graph := "digraph {\n ratio=\"compress\";size=\"8,4!\";splines=true;\"" + word + "\";\n"
graph = construct_graph_sub(graph, word, seen, 1) graph = construct_graph_sub(graph, word, seen, 1)
graph += "}" graph += "}"
ioutil.WriteFile("tmp.dot", []byte(graph), 0640) ioutil.WriteFile("tmp.dot", []byte(graph), 0640)
subProcess := exec.Command("dot", "-Tsvg", "-otmp.svg","-Nfontname=Chilanka", "tmp.dot") subProcess := exec.Command("dot", "-Tsvg", "-otmp.svg", "-Nfontname=Chilanka", "tmp.dot")
subProcess.Run() subProcess.Run()
subProcess.Wait() subProcess.Wait()
data, _ := ioutil.ReadFile("tmp.svg") data, _ := ioutil.ReadFile("tmp.svg")
return "<div class=\"conceptgraph\">"+strings.ReplaceAll(string(data), `<?xml version="1.0" encoding="UTF-8" standalone="no"?> return "<div class=\"conceptgraph\">" + strings.ReplaceAll(string(data), `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">`, "") "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">`, "")
} }
@ -181,7 +179,7 @@ func construct_graph_sub(graph string, word string, seen map[string]bool, depth
if depth == 1 { if depth == 1 {
col = "fillcolor=\"#b09cbc\"" col = "fillcolor=\"#b09cbc\""
} }
graph += fmt.Sprintf("\"%v\" [href=\"/view/%v\",%v,style=filled];\n", normalize(word), normalize(word),col) graph += fmt.Sprintf("\"%v\" [href=\"/view/%v\",%v,style=filled];\n", normalize(word), normalize(word), col)
} }
seen[normalize(word)] = true seen[normalize(word)] = true
@ -197,7 +195,6 @@ func construct_graph_sub(graph string, word string, seen map[string]bool, depth
graph += fmt.Sprintf("\"%v\" [href=\"/view/%v\"];\n", normalize(link), normalize(link)) graph += fmt.Sprintf("\"%v\" [href=\"/view/%v\"];\n", normalize(link), normalize(link))
} }
if normalize(word) != normalize(link) && (!seen[normalize(word)+"++"+normalize(link)] || !seen[normalize(link)+"++"+normalize(word)]) { if normalize(word) != normalize(link) && (!seen[normalize(word)+"++"+normalize(link)] || !seen[normalize(link)+"++"+normalize(word)]) {
if strings.HasPrefix(link, "typeof:") { if strings.HasPrefix(link, "typeof:") {
graph += fmt.Sprintf("\"%v\" -> \"%v\" [style=dotted]\n", link[7:], word) graph += fmt.Sprintf("\"%v\" -> \"%v\" [style=dotted]\n", link[7:], word)
@ -220,12 +217,10 @@ func construct_graph_sub(graph string, word string, seen map[string]bool, depth
} }
//} //}
if !seen[link] { if !seen[link] {
graph = construct_graph_sub(graph, link, seen, depth-1) graph = construct_graph_sub(graph, link, seen, depth-1)
} }
} }
return graph return graph