scholar

package module
v0.0.0-...-3ba6478 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2021 License: MIT Imports: 10 Imported by: 2

README

Scholar Library

Go library to generate and manipulate bibliography entries.

For the CLI program, click here.

Usage

Scholar uses a YAML configuration file to load the types of entries to be used. The basic structure of the file is as follows:

typeName:
  desc: A brief description of the type.
  req:
    field1: A brief description of field1.
    field2: A brief description of field2.
    field3: A brief description of field3.
  opt:
    field1: A brief description of field1.
    field2: A brief description of field2.
    field3: A brief description of field3.

For example:

article:
  desc: An article in a journal, magazine, newspaper, or other periodical which forms a self-contained unit with its own title.
  req:
    author: Author(s) of the article.
    title: Title of the article.
    journaltitle: Title of the journal.
    date: YYYY-MM-DD format.
  opt:
    editor: Editor(s) of the journal.
    language: Language of the article.
    series: Series of the journal.
    volume: Volume of the journal.
    number: Number of the journal.
    issn: ISSN number of the article.
    doi: DOI code of the article.
    url: URL of the article.
    urldate: Access date in YYY-MM-DD format.

book:
  desc: A single-volume book with one or more authors where the authors share credit for the work as a whole.
  req:
    author: Author(s) of the book.
    title: Title of the book.
    date: YYYY-MM-DD format.
  opt:
    editor: Editor(s) of the book.
    publisher: Publisher of the book.
    location: Location of the publisher.
    language: Language of the book.
    series: Series of the book.
    volume: Volume of the book.
    number: Number of the book.
    pages: Number of pages is the book.
    isbn: ISBN number of the book.
    doi: DOI code of the book.
    url: URL of the book.
    urldate: Access date in YYY-MM-DD format.

To load the entry types to be used by Scholar, do:

func main() {
    if err := scholar.LoadTypes("types.yaml"); err != nil {
        // handle error
    }    
}

To create a new Entry struct, do:

    entry := scholar.NewEntry("article")

To change the type of an entry, do:

    entryArticle := scholar.NewEntry("article")
    entryBook := scholar.Convert(entryArticle, "book")

To get a BibLaTex format reference, do:

    entry := scholar.NewEntry("article")
    entry.Required["title"] = "The Article"
    entry.Required["author'] = "Last, First and Other, Second"
    entry.Required["journaltitle"] = "The Journal of Articles"
    entry.Required["date"] = "2018"

    fmt.Println(entry.Bib())
output:
    @article{last2018,
      author = {Last, First and Other, Second},
      date = {2018},
      journaltitle = {The Journal of Articles},
      title = {The Article}
    }

TODO

  • Return an error on TypeNotFound for NewEntry()
  • Improve documentation

Documentation

Index

Constants

View Source
const (

	// ErrTypeNotFound represents an entry type not found error.
	ErrTypeNotFound errorType
	// ErrFieldNotFound represents a field not found error.
	ErrFieldNotFound
)

Variables

View Source
var EntryTypes map[string]*EntryType

EntryTypes holds the list of all entry types loaded.

View Source
var TypeNotFoundError *tnfError

TypeNotFoundError is deprecated.

Functions

func FTypesInfo

func FTypesInfo(w io.Writer, level int)

FTypesInfo writes the information of each entry type to the io.Writer w. The level indicates how much information to show (0 = only labels, 1 = labels and required fields, 2 = labels, required fields, and optional fields.)

func IsError

func IsError(eType errorType, err error) bool

IsError checks if err is an error of the given type.

func LoadTypes

func LoadTypes(file string) error

LoadTypes loads the configuration file for entry types and associated fields.

func TypesInfo

func TypesInfo(level int)

TypesInfo shows the information of each entry type. The level indicates how much information to show (0 = only labels, 1 = labels and required fields, 2 = labels, required fields, and optional fields.)

Types

type Entry

type Entry struct {
	Type     string            `yaml:"type"`
	Key      string            `yaml:"key"`
	Required map[string]string `yaml:"req"`
	Optional map[string]string `yaml:"opt"`
	File     string            `yaml:"file"`
	Info     os.FileInfo       `yaml:"-"`
}

Entry is the basic object of scholar.

func Convert

func Convert(e *Entry, entryType string) (*Entry, error)

Convert changes the type of an entry, parsing all the fields from one type to the other.

If the entry type does not exits, it returns with an ErrTypeNotFound error. If a required field from the output entry type cannot be found on the original entry, the Required field is replaced by an empty string and Convert keeps parsing the entry. An ErrFieldNotFound error is raised as a result, but it is safe to ignore ErrFieldNotFound.

Any field that was Required in the original entry, but it is not on the output entry type, is converted to an Optional field. This ensures back and forth conversion of the same entry without losing information.

func NewEntry

func NewEntry(label string) (*Entry, error)

NewEntry returns an empty copy of an entry according to the types of entries loaded.

func (*Entry) Attach

func (e *Entry) Attach(file string)

Attach attaches a file path to the entry.

func (*Entry) Bib

func (e *Entry) Bib() string

Bib returns a string with all the information of the entry in BibLaTex format.

func (*Entry) Check

func (e *Entry) Check() error

Check checks if the fields are formatted correctly. [Currently not useful]

func (*Entry) Export

func (e *Entry) Export(format string) string

Export returns a string with all the information of the entry in the given format.

func (*Entry) FirstAuthorLast

func (e *Entry) FirstAuthorLast() string

FirstAuthorLast return the lastname of the first author of the entry.

func (*Entry) GetKey

func (e *Entry) GetKey() string

GetKey return the key of the entry. If there is no key, a new key is generated with lastnameYEAR format. For example: einstein1922

func (*Entry) Year

func (e *Entry) Year() string

Year returns the year of the entry.

type EntryType

type EntryType struct {
	Type        string
	Description string            `yaml:"desc"`
	Required    map[string]string `yaml:"req"`
	Optional    map[string]string `yaml:"opt"`
}

EntryType defines how each entry will be formatted. Each entry has a TYPE of entry, a short DESCRIPTION, REQUIRED fields, and OPTIONAL fields according to BibLaTex documentation.

func (*EntryType) String

func (e *EntryType) String() string

String implements the Stringer interface.

type Err

type Err struct {
	// contains filtered or unexported fields
}

Err represents a custom error handler.

func (*Err) Error

func (e *Err) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL