Formatting
This commit is contained in:
parent
37bebe271e
commit
6543f89843
|
@ -0,0 +1,4 @@
|
||||||
|
tmp.dot
|
||||||
|
tmp.png
|
||||||
|
tmp.svg
|
||||||
|
.idea/
|
|
@ -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], ":")
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ func ExtractPDF(extract string) (string,string,string,*bibtex.BibEntry) {
|
||||||
id := sha512.Sum512([]byte(src))
|
id := sha512.Sum512([]byte(src))
|
||||||
fetch.Fetch(src, hex.EncodeToString(id[:]))
|
fetch.Fetch(src, hex.EncodeToString(id[:]))
|
||||||
cachepath := path.Join("cache/", hex.EncodeToString(id[:]))
|
cachepath := path.Join("cache/", hex.EncodeToString(id[:]))
|
||||||
args = append(args, "-singlefile", "-png", cachepath, "tmp")
|
args = append(args, "-singlefile", "-png", cachepath, "tmp")
|
||||||
subProcess := exec.Command("pdftoppm", args...)
|
subProcess := exec.Command("pdftoppm", args...)
|
||||||
log.Infof("Running.. %v", subProcess.Args, subProcess.String())
|
log.Infof("Running.. %v", subProcess.Args, subProcess.String())
|
||||||
err := subProcess.Run()
|
err := subProcess.Run()
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
73
kdb.go
73
kdb.go
|
@ -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"
|
||||||
|
@ -19,10 +18,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
Title string
|
Title string
|
||||||
Body template.HTML
|
Body template.HTML
|
||||||
BodyOrig string
|
BodyOrig string
|
||||||
Concept template.HTML
|
Concept template.HTML
|
||||||
References map[string]*bibtex.BibEntry
|
References map[string]*bibtex.BibEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
|
@ -104,7 +103,7 @@ func normalize(word string) string {
|
||||||
word = word[7:]
|
word = word[7:]
|
||||||
} else if strings.HasPrefix(word, "subtype:") {
|
} else if strings.HasPrefix(word, "subtype:") {
|
||||||
word = word[8:]
|
word = word[8:]
|
||||||
} else if strings.HasPrefix(word, "instance:") {
|
} else if strings.HasPrefix(word, "instance:") {
|
||||||
word = word[9:]
|
word = word[9:]
|
||||||
}
|
}
|
||||||
word = strings.TrimSpace(word)
|
word = strings.TrimSpace(word)
|
||||||
|
@ -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
|
||||||
|
@ -189,7 +187,7 @@ func construct_graph_sub(graph string, word string, seen map[string]bool, depth
|
||||||
graph += "# " + word + "\n"
|
graph += "# " + word + "\n"
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
|
|
||||||
if strings.HasPrefix(word, "typeof:") || strings.HasPrefix(word, "instance:") || strings.HasPrefix(word, "subtype:") {
|
if strings.HasPrefix(word, "typeof:") || strings.HasPrefix(word, "instance:") || strings.HasPrefix(word, "subtype:") {
|
||||||
word = normalize(word)
|
word = normalize(word)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,35 +195,32 @@ 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)
|
seen[word+"++"+link[7:]] = true
|
||||||
seen[word+"++"+link[7:]] = true
|
seen[link[7:]+"++"+word] = true
|
||||||
seen[link[7:]+"++"+word] = true
|
} else if strings.HasPrefix(link, "subtype:") {
|
||||||
} else if strings.HasPrefix(link, "subtype:") {
|
graph += fmt.Sprintf("\"%v\" -> \"%v\" [style=dotted]\n", word, link[8:])
|
||||||
graph += fmt.Sprintf("\"%v\" -> \"%v\" [style=dotted]\n", word, link[8:])
|
seen[word+"++"+link[8:]] = true
|
||||||
seen[word+"++"+link[8:]] = true
|
seen[link[8:]+"++"+word] = true
|
||||||
seen[link[8:]+"++"+word] = true
|
} else if strings.HasPrefix(link, "instance:") {
|
||||||
} else if strings.HasPrefix(link, "instance:") {
|
graph += fmt.Sprintf("\"%v\" -> \"%v\" [style=dotted]\n", word, link[9:])
|
||||||
graph += fmt.Sprintf("\"%v\" -> \"%v\" [style=dotted]\n", word, link[9:])
|
seen[word+"++"+link[9:]] = true
|
||||||
seen[word+"++"+link[9:]] = true
|
seen[link[9:]+"++"+word] = true
|
||||||
seen[link[9:]+"++"+word] = true
|
} else {
|
||||||
} else {
|
graph += fmt.Sprintf("\"%v\" -> \"%v\" []\n", word, link)
|
||||||
graph += fmt.Sprintf("\"%v\" -> \"%v\" []\n", word, link)
|
|
||||||
}
|
|
||||||
|
|
||||||
seen[normalize(word)+"++"+normalize(link)] = true
|
|
||||||
seen[normalize(link)+"++"+normalize(word)] = true
|
|
||||||
}
|
}
|
||||||
//}
|
|
||||||
|
|
||||||
|
seen[normalize(word)+"++"+normalize(link)] = true
|
||||||
|
seen[normalize(link)+"++"+normalize(word)] = true
|
||||||
|
}
|
||||||
|
//}
|
||||||
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue