Skip to content

Commit

Permalink
Add author display name format setting
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
shemanaev committed Apr 10, 2023
1 parent bc931bd commit 3b03d0b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
5 changes: 5 additions & 0 deletions inpxer-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
language = "en" # en, ru
# title displayed on web and in OPDS catalog
title = "My library"
# format for author's name. possible values:
# - short: Fyodor Dostoevsky
# - initials: F. M. Dostoevsky
# - full: Fyodor Mikhailovich Dostoevsky
author_name_format = "short"
# where to store index
# WARNING: keep in mind that this folder will be deleted during indexing,
# don't point it to an existing location (and definitely don't set it equal to library_path)
Expand Down
15 changes: 8 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
const configFilename = "inpxer.toml"

type MyConfig struct {
Language string `toml:"language"`
Title string `toml:"title"`
IndexPath string `toml:"index_path"`
LibraryPath string `toml:"library_path"`
Listen string `toml:"listen"`
FullUrl string `toml:"full_url"`
Converters []*Converter `toml:"converters"`
Language string `toml:"language"`
Title string `toml:"title"`
AuthorNameFormat string `toml:"author_name_format"`
IndexPath string `toml:"index_path"`
LibraryPath string `toml:"library_path"`
Listen string `toml:"listen"`
FullUrl string `toml:"full_url"`
Converters []*Converter `toml:"converters"`
}

type Converter struct {
Expand Down
11 changes: 11 additions & 0 deletions internal/model/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ func (a Author) Initials() string {
return name
}

func (a Author) FormattedName(format string) string {
switch format {
case "initials":
return a.Initials()
case "full":
return a.String()
default:
return a.Short()
}
}

func ToSlash(path string) string {
return strings.ReplaceAll(path, "\\", "/")
}
4 changes: 2 additions & 2 deletions internal/server/opds.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ func (h *OpdsHandler) makeBooksList(books []*model.Book) []*opds.Entry {
entry.Content = opds.NewText(h.t.Getf("Original title: %s", book.Title))

for _, author := range book.Authors {
entry.Author = append(entry.Author, opds.Author{Name: author.Short()})
entry.Author = append(entry.Author, opds.Author{Name: author.FormattedName(h.cfg.AuthorNameFormat)})
entry.Link = append(entry.Link, opds.Link{
Rel: opds.LinkRelRelated,
Type: opds.LinkTypeNavigation,
Href: "/opds/search?q=" + url.QueryEscape(author.String()),
Title: h.t.Getf("Search books by %s", author.Short()),
Title: h.t.Getf("Search books by %s", author.FormattedName(h.cfg.AuthorNameFormat)),
})
}

Expand Down
34 changes: 18 additions & 16 deletions internal/server/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ type resultStats struct {
}

type arguments struct {
T *spreak.Localizer
Converters []*config.Converter
Title string
Query string
Field string
Paginator pagination
Results resultStats
Hits []*model.Book
T *spreak.Localizer
Converters []*config.Converter
Title string
AuthorNameFormat string
Query string
Field string
Paginator pagination
Results resultStats
Hits []*model.Book
}

func NewWebHandler(cfg *config.MyConfig, localizer *spreak.Localizer) *WebHandler {
Expand Down Expand Up @@ -131,14 +132,15 @@ func (h *WebHandler) Search(w http.ResponseWriter, r *http.Request) {
}

args := arguments{
T: h.localizer,
Converters: h.cfg.Converters,
Title: h.cfg.Title,
Query: q,
Field: field,
Paginator: paginator,
Results: stats,
Hits: top.Hits,
T: h.localizer,
Converters: h.cfg.Converters,
Title: h.cfg.Title,
AuthorNameFormat: h.cfg.AuthorNameFormat,
Query: q,
Field: field,
Paginator: paginator,
Results: stats,
Hits: top.Hits,
}
if err := h.searchTpl.Execute(w, args); err != nil {
internalServerError(w)
Expand Down
2 changes: 1 addition & 1 deletion ui/templates/search.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<span class="book-details-row--value">
{{range .Authors}}
<a class="comma-separated" href="/search?q={{.}}&field=Authors"
itemprop="author" title="{{.}}">{{.Initials}}</a>
itemprop="author" title="{{.}}">{{.FormattedName $.AuthorNameFormat}}</a>
{{end}}
</span>
</div>
Expand Down

0 comments on commit 3b03d0b

Please sign in to comment.