asciidoccite

A simple bibliography generator for Asciidoc using local Zotero

https://github.com/hesusruiz/asciidoccite

Science Score: 31.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (0.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation ·

Repository

A simple bibliography generator for Asciidoc using local Zotero

Basic Info
  • Host: GitHub
  • Owner: hesusruiz
  • License: apache-2.0
  • Language: Go
  • Default Branch: master
  • Size: 8.79 KB
Statistics
  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created almost 7 years ago · Last pushed almost 7 years ago
Metadata Files
Readme License Citation

README.md

AsciidocCite

A simple bibliography generator for Asciidoc using local Zotero

Owner

  • Name: Jesus Ruiz
  • Login: hesusruiz
  • Kind: user

Citation (citations.go)

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"regexp"
	"sort"
	"strings"
)

// AuthorType holds family and given name
type AuthorType struct {
	Family string `json:"family"`
	Given  string `json:"given"`
}

// IssuedType holds the date
type IssuedType struct {
	DateParts [][]interface{} `json:"date-parts"`
}

// ResultType holds the real reply
type ResultType struct {
	Page           string       `json:"page"`
	Title          string       `json:"title"`
	ContainerTitle string       `json:"container-title"`
	Author         []AuthorType `json:"author"`
	Issued         IssuedType   `json:"issued"`
	DOI            string       `json:"DOI"`
}

// ZoteroReply defines the parsed JSON stream
type ZoteroReply struct {
	Jsonrpc string       `json:"jsonrpc"`
	Result  []ResultType `json:"result"`
}

func getBibliographyFromCitekey(citekey string) (ResultType, error) {
	// Request message to Zotero local server
	unformattedRequest := "{\"jsonrpc\": \"2.0\", \"method\": \"item.search\", \"params\": [\"%s\"] }"
	formattedRequest := fmt.Sprintf(unformattedRequest, citekey)
	requestMessage := strings.NewReader(formattedRequest)

	// Create an http request object to be able to set the headers
	req, err := http.NewRequest(
		"POST",
		"http://localhost:23119/better-bibtex/json-rpc",
		requestMessage)
	if err != nil {
		return ResultType{}, err
	}

	// The request body will be in JSON format
	req.Header.Add("Content-Type", "application/json")

	// Send the actual request to the server and receive the reply
	httpClient := &http.Client{}
	resp, err := httpClient.Do(req)
	if err != nil {
		return ResultType{}, err
	}

	// Read everything
	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ResultType{}, err
	}

	var reply ZoteroReply

	// Decode answer as JSON
	err = json.Unmarshal(responseBody, &reply)
	if err != nil {
		return ResultType{}, err
	}

	// reply.Result should be an array of ResultType
	// Check that we have at least one item
	if len(reply.Result) == 0 {
		log.Fatalln("There were no results to the query")
	}

	// get the contents of the first result
	r := reply.Result[0]
	return r, nil

}

func buildAsciidocBibliographyItem(citekey string, index int, result ResultType) string {

	var b strings.Builder

	// Initial part of bibliography line
	fmt.Fprintf(&b, "- [[[%s, %s]]] ", citekey, citekey)

	// Add the authors
	for i, author := range result.Author {
		fmt.Fprintf(&b, "%s %s", author.Given, author.Family)
		if i < len(result.Author)-1 {
			b.WriteString(" and ")
		} else {
			b.WriteString(". ")
		}
	}

	// Add the title
	fmt.Fprintf(&b, "\"%s\"", result.Title)

	// Add the date issued
	fmt.Fprintf(&b, " (%s).", result.Issued.DateParts[0][0])

	// Add the container title
	if len(result.ContainerTitle) > 0 {
		fmt.Fprintf(&b, " %s.", result.ContainerTitle)
	}

	// Add the DOI
	if len(result.DOI) > 0 {
		fmt.Fprintf(&b, " DOI: %s.", result.DOI)
	}

	return b.String()

}

func main() {

	// Require the first argument to be the name of a file to process
	if len(os.Args) < 2 {
		fmt.Println("A file name is required")
		os.Exit(1)
	}
	fileName := string(os.Args[1])

	// Define the regex for detecting the citekeys in the Asciidoc document
	re := regexp.MustCompile(`<<.+?>>`)

	// Read the file entirely in memory
	content, err := ioutil.ReadFile(fileName)
	if err != nil {
		log.Fatal(err)
	}

	// Find all citation keys
	citekeys := re.FindAll([]byte(content), -1)

	// Sort alfabetically the citekeys
	sort.Slice(citekeys, func(i, j int) bool {
		return string(citekeys[i]) < string(citekeys[j])
	})

	// Print the bibliography to stdout (can be copy/pasted)
	for i, citekey := range citekeys {
		c := strings.Trim(string(citekey), "<>")

		// get the bibliography data from local Zotero
		r, err := getBibliographyFromCitekey(c)
		if err != nil {
			log.Fatal(err)
		}

		// Build the text line for the item
		s := buildAsciidocBibliographyItem(c, i, r)

		// Print to stdout
		fmt.Printf("%s\n\n", s)

	}

}

GitHub Events

Total
Last Year

Packages

  • Total packages: 1
  • Total downloads: unknown
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 0
proxy.golang.org: github.com/hesusruiz/asciidoccite
  • Versions: 0
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 7.0%
Average: 7.2%
Dependent repos count: 7.5%
Last synced: about 1 year ago