Commit: bfc8c1fadf575dbf7d41cc4a8f1596a05eadf358 Parent: 5d6f8fae4aad168383a41fd5a1f80a16a9884d62 Author: Vi Grey Date: 2024-01-31 05:41 UTC Summary: Change gemtext extension to gophermap or html - Change .gmi/.gemini to .html/.gophermap when generating http/gopher version of RSS feed - Fix year in CHANGELOG.md - Fix version number in src/rodor-logbook.go CHANGELOG.md | 30 +++++++++++++++++++++--------- src/gmi.go | 26 ++++++++++++++++++++++++++ src/io.go | 47 +++++++---------------------------------------- src/regex.go | 1 + src/rodor-logbook.go | 8 ++++++-- src/rss.go | 35 ++++++++++++++++++++++++++++++----- 6 files changed, 91 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9c0982..41a3b74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) -## [0.0.9] - 2022-11-04 +## [0.0.10] - 2024-01-31 + +### Added + +- Change .gmi/.gemini to .html/.gophermap when generating http/gopher version of RSS feed + +### Fixed + +- Year in CHANGELOG.md +- Version number in src/rodor-logbook.go + + +## [0.0.9] - 2023-11-04 ### Added @@ -14,21 +26,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Version numbers in CHANGELOG -## [0.0.8] - 2022-11-03 +## [0.0.8] - 2023-11-03 ### Added - tabindex="0" in pre tags for accessibility -## [0.0.7] - 2022-11-03 +## [0.0.7] - 2023-11-03 ### Removed - textOnly in html.go -## [0.0.6] - 2022-11-03 +## [0.0.6] - 2023-11-03 ### Removed @@ -39,7 +51,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Better schemeText handling for HTML -## [0.0.5] - 2022-09-12 +## [0.0.5] - 2023-09-12 ### Changed @@ -48,21 +60,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Only use CDATA for html -## [0.0.4] - 2022-09-01 +## [0.0.4] - 2023-09-01 ### Fixed - Revert Print to Printf for outer RSS and items -## [0.0.3] - 2022-09-01 +## [0.0.3] - 2023-09-01 ### Fixed - Printf to Print -## [0.0.2] - 2022-08-18 +## [0.0.2] - 2023-08-18 ### Fixed @@ -73,7 +85,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Go version down to 1.19 -## [0.0.1] - 2022-08-18 +## [0.0.1] - 2023-08-18 ### Added diff --git a/src/gmi.go b/src/gmi.go index 79b9dfb..e4192cd 100644 --- a/src/gmi.go +++ b/src/gmi.go @@ -1,6 +1,7 @@ package main import ( + "path/filepath" "strings" ) @@ -83,3 +84,28 @@ func parseGemtextLine(line string, preformattedToggle bool) (g gemtextLine) { } return } + +func gemtextFileExists(fullPath string) (filePath string, hasExtension, exists bool) { + // Check for path (which already ends in .gemini or .gmi) + matchList, _ := filepath.Glob(fullPath) + for _, match := range matchList { + if gmiExtensionRe.MatchString(match) { + return match, true, true + } + } + // Check for path + .gemini or path + .gmi + matchList, _ = filepath.Glob(fullPath + ".*") + for _, match := range matchList { + if gmiExtensionRe.MatchString(match) { + return match, false, true + } + } + // Check for path + /index.gemini or path + /index.gmi + matchList, _ = filepath.Glob(filepath.Join(fullPath, "index.*")) + for _, match := range matchList { + if gmiExtensionRe.MatchString(match) { + return match, false, true + } + } + return +} diff --git a/src/io.go b/src/io.go index ed2dd21..123503f 100644 --- a/src/io.go +++ b/src/io.go @@ -1,51 +1,18 @@ package main import ( - "os" "path/filepath" + "strings" ) -var ( - gmiExtensions = []string{".gmi", ".gemtext", ".GMI", ".GEMTEXT"} -) +func indexlessPath(p string) string { + if dir, file := filepath.Split(p); strings.ToLower(file) == "index" { + return filepath.Clean(dir) + } + return p +} func getActualPath(p string) (pNew string) { pNew = filepath.Join(basedir, p) return } - -/* -Check if: - - direct file exists - - direct file + .gmi, .gemtext, .GMI, .GEMTEXT exists - - direct file + /index + .gmi, .gemtext, .GMI, .GEMTEXT exists - -Returns the path with the extension of the gemtext file -*/ -func getGMIFilePath(fullPath string) string { - // Direct file check - if fileInfo, err := os.Stat(fullPath); err == nil { - if !fileInfo.IsDir() { - return fullPath - } - } - // File sans extension check - for _, extension := range gmiExtensions { - extensionPath := fullPath + extension - if fileInfo, err := os.Stat(extensionPath); err == nil { - if !fileInfo.IsDir() { - return extensionPath - } - } - } - // Index file check - for _, extension := range gmiExtensions { - indexPath := filepath.Join(fullPath, "index"+extension) - if fileInfo, err := os.Stat(indexPath); err == nil { - if !fileInfo.IsDir() { - return indexPath - } - } - } - return "" -} diff --git a/src/regex.go b/src/regex.go index 70e74cd..8e78f5f 100644 --- a/src/regex.go +++ b/src/regex.go @@ -50,4 +50,5 @@ var ( `)?` + `(.+)$`) // Title capture group gemtextLinkFeedRe = regexp.MustCompile(gemtextLinkFeedTxt) + gmiExtensionRe = regexp.MustCompile(`(?i)^.*\.(?:gemini|gmi)$`) ) diff --git a/src/rodor-logbook.go b/src/rodor-logbook.go index 261f94f..a5d6a61 100644 --- a/src/rodor-logbook.go +++ b/src/rodor-logbook.go @@ -8,7 +8,7 @@ import ( const ( PROGRAM = "rodor-logbook" - VERSION = "0.0.8" + VERSION = "0.0.10" ) func main() { @@ -16,7 +16,11 @@ func main() { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - p := getGMIFilePath(getActualPath(inputpath)) + p, _, exists := gemtextFileExists(getActualPath(inputpath)) + if !exists { + fmt.Fprintln(os.Stderr, fmt.Sprintf("Unable to find %s", inputpath)) + os.Exit(1) + } f, err := os.OpenFile(p, os.O_RDONLY, 0444) if err != nil { panic(err) diff --git a/src/rss.go b/src/rss.go index f46e85b..92b0a2b 100644 --- a/src/rss.go +++ b/src/rss.go @@ -17,12 +17,24 @@ func printRSSData(gmiLinks []gemtextLink) { os.Exit(1) } var link string + linkInputPath := inputpath + if _, hasExtension, _ := gemtextFileExists(getActualPath(inputpath)); hasExtension { + linkInputPath = strings.TrimSuffix(inputpath, filepath.Ext(inputpath)) + switch strings.ToLower(u.Scheme) { + case "gopher": + linkInputPath += ".gophermap" + case "http", "https": + linkInputPath += ".html" + case "gemini": + linkInputPath = inputpath + } + } if strings.ToLower(u.Scheme) == "gopher" { link = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, - filepath.Join("/1", inputpath)) + filepath.Join("/1", linkInputPath)) } else { link = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, - filepath.Join("/", inputpath)) + filepath.Join("/", linkInputPath)) } lastBuildDate := time.Now().UTC().Format(time.RFC1123Z) fmt.Printf(` @@ -42,12 +54,24 @@ func printRSSData(gmiLinks []gemtextLink) { for _, link := range gmiLinks { postTitle := link.title var postLink string + linkPath := link.path + if _, hasExtension, _ := gemtextFileExists(getActualPath(link.path)); hasExtension { + linkPath = strings.TrimSuffix(link.path, filepath.Ext(link.path)) + switch strings.ToLower(u.Scheme) { + case "gopher": + linkPath += ".gophermap" + case "http", "https": + linkPath += ".html" + case "gemini": + linkPath = link.path + } + } if strings.ToLower(u.Scheme) == "gopher" { postLink = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, - filepath.Join("/1", link.path)) + filepath.Join("/1", linkPath)) } else { postLink = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, - filepath.Join("/", link.path)) + filepath.Join("/", linkPath)) } fmt.Printf(` %s @@ -59,7 +83,8 @@ func printRSSData(gmiLinks []gemtextLink) { escapeHTMLContent(postLink), escapeHTMLContent(postLink), link.date.UTC().Format(time.RFC1123Z)) - f, err := os.OpenFile(getGMIFilePath(getActualPath(link.path)), os.O_RDONLY, 0444) + p, _, _ := gemtextFileExists(getActualPath(link.path)) + f, err := os.OpenFile(p, os.O_RDONLY, 0444) if err != nil { panic(err) }