Add additional regexes

This commit is contained in:
Alex Yatskov 2023-11-03 16:58:36 -07:00
parent db2d236d17
commit 9af373fdab
2 changed files with 28 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package mex
import ( import (
"bytes" "bytes"
_ "embed"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
@ -9,11 +10,29 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
) )
//go:embed regexp.txt
var volumeExpStrs string
func parseVolumeIndex(path string) *int {
for _, expStr := range strings.Split(volumeExpStrs, "\n") {
exp := regexp.MustCompile(expStr)
if matches := exp.FindStringSubmatch(filepath.Base(path)); len(matches) >= 2 {
if index, err := strconv.ParseInt(matches[1], 10, 32); err == nil {
indexInt := int(index)
return &indexInt
}
}
}
return nil
}
func isImagePath(path string) bool { func isImagePath(path string) bool {
switch strings.ToLower(filepath.Ext(path)) { switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif": case ".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif":
@ -272,14 +291,8 @@ func (self *Book) parseVolumes(node *Node) {
} }
if len(volume.Pages) > 0 { if len(volume.Pages) > 0 {
exp := regexp.MustCompile(`(\d+)\D*$`) if index := parseVolumeIndex(node.Name); index != nil {
if matches := exp.FindStringSubmatch(node.Name); len(matches) >= 2 { volume.Index = *index
index, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
panic(err)
}
volume.Index = int(index)
self.addVolume(volume) self.addVolume(volume)
} else { } else {
self.addOrphan(volume) self.addOrphan(volume)
@ -296,6 +309,10 @@ func ParseBook(node *Node) (*Book, error) {
book.parseVolumes(node) book.parseVolumes(node)
if len(book.orphans) > 0 { if len(book.orphans) > 0 {
sort.Slice(book.orphans, func(i, j int) bool {
return strings.Compare(book.orphans[i].Node.Name, book.orphans[j].Node.Name) < 0
})
for _, volume := range book.orphans { for _, volume := range book.orphans {
volume.Index = book.VolumeCount volume.Index = book.VolumeCount
book.addVolume(volume) book.addVolume(volume)

3
regexp.txt Normal file
View File

@ -0,0 +1,3 @@
第\s*(\d+)\s*巻
(?i)vo?l?u?m?e?[.\s]*(\d+)
`(\d+)\D*$`