Compare commits
3 Commits
25c39036d3
...
v0.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| af3bdd7d9a | |||
| d3c36c9e74 | |||
| 8126ded0a6 |
22
README.md
22
README.md
@@ -12,4 +12,24 @@ Running:
|
|||||||
|
|
||||||
```shell
|
```shell
|
||||||
./dist/gmi2html <gemtext.gmi >gemtext.html
|
./dist/gmi2html <gemtext.gmi >gemtext.html
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Options:
|
||||||
|
|
||||||
|
- `--no-container`: Don't output container HTML
|
||||||
|
- `--replace-gmi-ext`: Replace .gmi extension with .html in links
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# Convert Gemini text and replace all .gmi links with .html
|
||||||
|
./dist/gmi2html --replace-gmi-ext <input.gmi >output.html
|
||||||
|
|
||||||
|
# Convert only the content without wrapping it in the HTML container
|
||||||
|
./dist/gmi2html --no-container <input.gmi >output-content.html
|
||||||
|
```
|
||||||
|
|
||||||
|
Help:
|
||||||
|
```shell
|
||||||
|
./dist/gmi2html --help
|
||||||
|
```
|
||||||
|
|||||||
127
assets/main.html
Normal file
127
assets/main.html
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{{.Title}}</title>
|
||||||
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source+Serif+Pro:ital,wght@0,400;0,600;0,700;1,400&display=swap">
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--text-color: #333;
|
||||||
|
--bg-color: #fff;
|
||||||
|
--link-color: #0066cc;
|
||||||
|
--link-hover: #004080;
|
||||||
|
--quote-bg: #f5f5f5;
|
||||||
|
--quote-border: #ddd;
|
||||||
|
--pre-bg: #f8f8f8;
|
||||||
|
--pre-border: #eaeaea;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root {
|
||||||
|
--text-color: #eee;
|
||||||
|
--bg-color: #292929;
|
||||||
|
--link-color: #4a9eff;
|
||||||
|
--link-hover: #77b6ff;
|
||||||
|
--quote-bg: #333;
|
||||||
|
--quote-border: #444;
|
||||||
|
--pre-bg: #2a2a2a;
|
||||||
|
--pre-border: #3a3a3a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: "Source Serif Pro", "Georgia", "Cambria", serif;
|
||||||
|
color: var(--text-color);
|
||||||
|
/* background-color: var(--bg-color); */
|
||||||
|
max-width: 34rem;
|
||||||
|
/* margin: 0 auto; */
|
||||||
|
margin-left: 2rem;
|
||||||
|
padding: 1rem 1rem;
|
||||||
|
font-size: 16px;
|
||||||
|
text-align: justify;
|
||||||
|
hyphens: auto;
|
||||||
|
-webkit-hyphens: auto;
|
||||||
|
-ms-hyphens: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-container {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-heading-1 {
|
||||||
|
font-size: 2rem;
|
||||||
|
margin-top: 1rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
font-weight: 700;
|
||||||
|
line-height: 1.2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-heading-2 {
|
||||||
|
font-size: 1.6rem;
|
||||||
|
margin-top: 0.8rem;
|
||||||
|
margin-bottom: 0.8rem;
|
||||||
|
font-weight: 600;
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-heading-3 {
|
||||||
|
font-size: 1.3rem;
|
||||||
|
margin-top: 0.7rem;
|
||||||
|
margin-bottom: 0.7rem;
|
||||||
|
font-weight: 600;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-textline {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-list-item {
|
||||||
|
margin: 0.5rem 0;
|
||||||
|
padding-left: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-link-container {
|
||||||
|
margin: 1rem 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-link-container a {
|
||||||
|
color: var(--link-color);
|
||||||
|
text-decoration: none;
|
||||||
|
border-bottom: 1px solid transparent;
|
||||||
|
transition: border-color 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-link-container a:hover {
|
||||||
|
color: var(--link-hover);
|
||||||
|
border-bottom-color: var(--link-hover);
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-blockquote {
|
||||||
|
background-color: var(--quote-bg);
|
||||||
|
border-left: 3px solid var(--quote-border);
|
||||||
|
margin: 1.5rem 0;
|
||||||
|
padding: 0.8rem 1rem;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-preformatted {
|
||||||
|
background-color: var(--pre-bg);
|
||||||
|
border: 1px solid var(--pre-border);
|
||||||
|
border-radius: 3px;
|
||||||
|
padding: 1rem;
|
||||||
|
overflow-x: auto;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
margin: 1rem 0;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="gemini-container">
|
||||||
|
{{.Content}}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -22,7 +22,7 @@ func runApp() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
html, err := gmi2html.Gmi2html(string(data), "")
|
html, err := gmi2html.Gmi2html(string(data), "", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
38
cmd/gmi2html/main.go
Normal file
38
cmd/gmi2html/main.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/antanst/gmi2html/pkg/gmi2html"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
noContainer := flag.Bool("no-container", false, "Don't output container HTML")
|
||||||
|
replaceGmiExt := flag.Bool("replace-gmi-ext", false, "Replace .gmi extension with .html in links")
|
||||||
|
flag.Parse()
|
||||||
|
err := runApp(*noContainer, *replaceGmiExt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runApp(noContainer bool, replaceGmiExt bool) error {
|
||||||
|
data, err := io.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
html, err := gmi2html.Gmi2html(string(data), "", noContainer, replaceGmiExt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = fmt.Fprintf(os.Stdout, "%s", html)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -12,10 +12,13 @@ import (
|
|||||||
// Based on https://geminiprotocol.net/docs/gemtext-specification.gmi
|
// Based on https://geminiprotocol.net/docs/gemtext-specification.gmi
|
||||||
|
|
||||||
// Gmi2html converts Gemini text to HTML with proper escaping and wraps it in a container with typography-focused CSS
|
// Gmi2html converts Gemini text to HTML with proper escaping and wraps it in a container with typography-focused CSS
|
||||||
func Gmi2html(text string, title string) (string, error) {
|
func Gmi2html(text string, title string, contentOnly bool) (string, error) {
|
||||||
content := convertGeminiContent(text)
|
content := convertGeminiContent(text)
|
||||||
|
|
||||||
// Handle any template errors with container
|
if contentOnly {
|
||||||
|
return content, nil
|
||||||
|
}
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
err := containerTmpl.Execute(&buffer, struct {
|
err := containerTmpl.Execute(&buffer, struct {
|
||||||
Title string
|
Title string
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ func TestConvertGeminiContent(t *testing.T) {
|
|||||||
|
|
||||||
func TestGmi2html(t *testing.T) {
|
func TestGmi2html(t *testing.T) {
|
||||||
sample := "# Hello Gemini\n\nThis is a test document.\n\n=> https://gemini.circumlunar.space/ Project Gemini"
|
sample := "# Hello Gemini\n\nThis is a test document.\n\n=> https://gemini.circumlunar.space/ Project Gemini"
|
||||||
result, _ := Gmi2html(sample, "Gemini Test")
|
result, _ := Gmi2html(sample, "Gemini Test", false)
|
||||||
|
|
||||||
// Check that it contains the expected elements
|
// Check that it contains the expected elements
|
||||||
if !strings.Contains(result, "<title>Gemini Test</title>") {
|
if !strings.Contains(result, "<title>Gemini Test</title>") {
|
||||||
|
|||||||
127
pkg/gmi2html/assets/main.html
Normal file
127
pkg/gmi2html/assets/main.html
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{{.Title}}</title>
|
||||||
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source+Serif+Pro:ital,wght@0,400;0,600;0,700;1,400&display=swap">
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--text-color: #333;
|
||||||
|
--bg-color: #fff;
|
||||||
|
--link-color: #0066cc;
|
||||||
|
--link-hover: #004080;
|
||||||
|
--quote-bg: #f5f5f5;
|
||||||
|
--quote-border: #ddd;
|
||||||
|
--pre-bg: #f8f8f8;
|
||||||
|
--pre-border: #eaeaea;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root {
|
||||||
|
--text-color: #eee;
|
||||||
|
--bg-color: #292929;
|
||||||
|
--link-color: #4a9eff;
|
||||||
|
--link-hover: #77b6ff;
|
||||||
|
--quote-bg: #333;
|
||||||
|
--quote-border: #444;
|
||||||
|
--pre-bg: #2a2a2a;
|
||||||
|
--pre-border: #3a3a3a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: "Source Serif Pro", "Georgia", "Cambria", serif;
|
||||||
|
color: var(--text-color);
|
||||||
|
/* background-color: var(--bg-color); */
|
||||||
|
max-width: 34rem;
|
||||||
|
/* margin: 0 auto; */
|
||||||
|
margin-left: 2rem;
|
||||||
|
padding: 1rem 1rem;
|
||||||
|
font-size: 16px;
|
||||||
|
text-align: justify;
|
||||||
|
hyphens: auto;
|
||||||
|
-webkit-hyphens: auto;
|
||||||
|
-ms-hyphens: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-container {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-heading-1 {
|
||||||
|
font-size: 2rem;
|
||||||
|
margin-top: 1rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
font-weight: 700;
|
||||||
|
line-height: 1.2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-heading-2 {
|
||||||
|
font-size: 1.6rem;
|
||||||
|
margin-top: 0.8rem;
|
||||||
|
margin-bottom: 0.8rem;
|
||||||
|
font-weight: 600;
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-heading-3 {
|
||||||
|
font-size: 1.3rem;
|
||||||
|
margin-top: 0.7rem;
|
||||||
|
margin-bottom: 0.7rem;
|
||||||
|
font-weight: 600;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-textline {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-list-item {
|
||||||
|
margin: 0.5rem 0;
|
||||||
|
padding-left: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-link-container {
|
||||||
|
margin: 1rem 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-link-container a {
|
||||||
|
color: var(--link-color);
|
||||||
|
text-decoration: none;
|
||||||
|
border-bottom: 1px solid transparent;
|
||||||
|
transition: border-color 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-link-container a:hover {
|
||||||
|
color: var(--link-hover);
|
||||||
|
border-bottom-color: var(--link-hover);
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-blockquote {
|
||||||
|
background-color: var(--quote-bg);
|
||||||
|
border-left: 3px solid var(--quote-border);
|
||||||
|
margin: 1.5rem 0;
|
||||||
|
padding: 0.8rem 1rem;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gemini-preformatted {
|
||||||
|
background-color: var(--pre-bg);
|
||||||
|
border: 1px solid var(--pre-border);
|
||||||
|
border-radius: 3px;
|
||||||
|
padding: 1rem;
|
||||||
|
overflow-x: auto;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
margin: 1rem 0;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="gemini-container">
|
||||||
|
{{.Content}}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
159
pkg/gmi2html/gmi2html.go
Normal file
159
pkg/gmi2html/gmi2html.go
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
package gmi2html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Based on https://geminiprotocol.net/docs/gemtext-specification.gmi
|
||||||
|
|
||||||
|
//go:embed assets/main.html
|
||||||
|
var rawTemplate string
|
||||||
|
|
||||||
|
// Gmi2html converts Gemini text to HTML with proper escaping and wraps it in a container with typography-focused CSS
|
||||||
|
func Gmi2html(text string, title string, contentOnly bool, replaceGmiExt bool) (string, error) {
|
||||||
|
content := convertGeminiContent(text, replaceGmiExt)
|
||||||
|
|
||||||
|
if contentOnly {
|
||||||
|
return content, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl := template.Must(template.New("gemini").Parse(rawTemplate))
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
err := tmpl.Execute(&buffer, struct {
|
||||||
|
Title string
|
||||||
|
Content template.HTML
|
||||||
|
}{
|
||||||
|
Title: title,
|
||||||
|
Content: template.HTML(content), // Content already properly escaped in convertGeminiContent
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error executing container template: %s\n", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// convertGeminiContent converts Gemini text to HTML with proper escaping
|
||||||
|
func convertGeminiContent(text string, replaceGmiExt bool) string {
|
||||||
|
lines := strings.Split(text, "\n")
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
normalMode := true
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(line, "=>"):
|
||||||
|
handleLinkLine(&buffer, line, replaceGmiExt)
|
||||||
|
case strings.HasPrefix(line, "```"):
|
||||||
|
if normalMode {
|
||||||
|
err := preformattedTmplStart.Execute(&buffer, line)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
buffer.WriteString("\n")
|
||||||
|
} else {
|
||||||
|
err := preformattedTmplEnd.Execute(&buffer, line)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
normalMode = !normalMode
|
||||||
|
// Don't output the ``` line
|
||||||
|
case strings.HasPrefix(line, "###"):
|
||||||
|
content := strings.TrimSpace(strings.TrimPrefix(line, "###"))
|
||||||
|
err := h3Tmpl.Execute(&buffer, content)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "##"):
|
||||||
|
content := strings.TrimSpace(strings.TrimPrefix(line, "##"))
|
||||||
|
err := h2Tmpl.Execute(&buffer, content)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "#"):
|
||||||
|
content := strings.TrimSpace(strings.TrimPrefix(line, "#"))
|
||||||
|
err := h1Tmpl.Execute(&buffer, content)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "*"):
|
||||||
|
content := strings.TrimSpace(strings.TrimPrefix(line, "*"))
|
||||||
|
err := listItemTmpl.Execute(&buffer, content)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, ">"):
|
||||||
|
content := strings.TrimSpace(strings.TrimPrefix(line, ">"))
|
||||||
|
err := blockquoteTmpl.Execute(&buffer, content)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if normalMode {
|
||||||
|
err := textLineTmpl.Execute(&buffer, line)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer.WriteString(line)
|
||||||
|
buffer.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleLinkLine parses and renders a link line
|
||||||
|
func handleLinkLine(buffer *bytes.Buffer, linkLine string, replaceGmiExt bool) {
|
||||||
|
url, description, err := parseGeminiLink(linkLine, replaceGmiExt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error parsing gemini link line: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = linkTmpl.Execute(buffer, struct {
|
||||||
|
URL, Description string
|
||||||
|
}{url, description})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseGeminiLink extracts URL and description from a link line
|
||||||
|
func parseGeminiLink(linkLine string, replaceGmiExt bool) (string, string, error) {
|
||||||
|
re := regexp.MustCompile(`^=>[ \t]+(\S+)([ \t]+.*)?`)
|
||||||
|
matches := re.FindStringSubmatch(linkLine)
|
||||||
|
if len(matches) == 0 {
|
||||||
|
return "", "", fmt.Errorf("error parsing link line: no regexp match for line %s", linkLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
urlStr := matches[1]
|
||||||
|
|
||||||
|
// Check: Unescape the URL if escaped
|
||||||
|
_, err := url.QueryUnescape(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("error parsing link line: %w input '%s'", err, linkLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace .gmi extension with .html if requested
|
||||||
|
if replaceGmiExt && strings.HasSuffix(urlStr, ".gmi") {
|
||||||
|
urlStr = strings.TrimSuffix(urlStr, ".gmi") + ".html"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set description to URL if not provided
|
||||||
|
description := urlStr
|
||||||
|
if len(matches) > 2 && strings.TrimSpace(matches[2]) != "" {
|
||||||
|
description = strings.TrimSpace(matches[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
return urlStr, description, nil
|
||||||
|
}
|
||||||
179
pkg/gmi2html/gmi2html_test.go
Normal file
179
pkg/gmi2html/gmi2html_test.go
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
package gmi2html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGmi2html(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
title string
|
||||||
|
contentOnly bool
|
||||||
|
replaceGmiExt bool
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Basic text",
|
||||||
|
input: "Hello world",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<p class=\"gemini-textline\">Hello world</p>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Headers",
|
||||||
|
input: "# Header 1\n## Header 2\n### Header 3",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<h1 class=\"gemini-heading-1\">Header 1</h1><h2 class=\"gemini-heading-2\">Header 2</h2><h3 class=\"gemini-heading-3\">Header 3</h3>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "List items",
|
||||||
|
input: "* Item 1\n* Item 2",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<p class=\"gemini-list-item\">• Item 1</p><p class=\"gemini-list-item\">• Item 2</p>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Blockquote",
|
||||||
|
input: "> This is a quote",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<blockquote class=\"gemini-blockquote\">This is a quote</blockquote>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Link",
|
||||||
|
input: "=> https://example.com Example Link",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<div class=\"gemini-link-container\"><a href=\"https://example.com\">Example Link</a></div>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Link with gmi extension replacement",
|
||||||
|
input: "=> /path/file.gmi Example Link",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: true,
|
||||||
|
want: "<div class=\"gemini-link-container\"><a href=\"/path/file.html\">Example Link</a></div>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Preformatted text",
|
||||||
|
input: "```\nThis is preformatted\n```",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<pre class=\"gemini-preformatted\">\nThis is preformatted\n</pre>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Mixed content",
|
||||||
|
input: "# Title\nNormal text\n=> https://example.com Link\n```\nCode\n```\n* List item",
|
||||||
|
title: "Test",
|
||||||
|
contentOnly: true,
|
||||||
|
replaceGmiExt: false,
|
||||||
|
want: "<h1 class=\"gemini-heading-1\">Title</h1><p class=\"gemini-textline\">Normal text</p><div class=\"gemini-link-container\"><a href=\"https://example.com\">Link</a></div><pre class=\"gemini-preformatted\">\nCode\n</pre><p class=\"gemini-list-item\">• List item</p>",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := Gmi2html(tt.input, tt.title, tt.contentOnly, tt.replaceGmiExt)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Gmi2html() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("Gmi2html() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseGeminiLink(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
linkLine string
|
||||||
|
replaceGmiExt bool
|
||||||
|
wantURL string
|
||||||
|
wantDesc string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Basic link",
|
||||||
|
linkLine: "=> https://example.com Example Link",
|
||||||
|
replaceGmiExt: false,
|
||||||
|
wantURL: "https://example.com",
|
||||||
|
wantDesc: "Example Link",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Link without description",
|
||||||
|
linkLine: "=> https://example.com",
|
||||||
|
replaceGmiExt: false,
|
||||||
|
wantURL: "https://example.com",
|
||||||
|
wantDesc: "https://example.com",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid link format",
|
||||||
|
linkLine: "Invalid line",
|
||||||
|
replaceGmiExt: false,
|
||||||
|
wantURL: "",
|
||||||
|
wantDesc: "",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Link with .gmi extension, no replacement",
|
||||||
|
linkLine: "=> /path/file.gmi Link to Gemini file",
|
||||||
|
replaceGmiExt: false,
|
||||||
|
wantURL: "/path/file.gmi",
|
||||||
|
wantDesc: "Link to Gemini file",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Link with .gmi extension, with replacement",
|
||||||
|
linkLine: "=> /path/file.gmi Link to Gemini file",
|
||||||
|
replaceGmiExt: true,
|
||||||
|
wantURL: "/path/file.html",
|
||||||
|
wantDesc: "Link to Gemini file",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Link without .gmi extension, with replacement",
|
||||||
|
linkLine: "=> /path/file.txt Link to text file",
|
||||||
|
replaceGmiExt: true,
|
||||||
|
wantURL: "/path/file.txt",
|
||||||
|
wantDesc: "Link to text file",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
gotURL, gotDesc, err := parseGeminiLink(tt.linkLine, tt.replaceGmiExt)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("parseGeminiLink() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if gotURL != tt.wantURL {
|
||||||
|
t.Errorf("parseGeminiLink() gotURL = %v, want %v", gotURL, tt.wantURL)
|
||||||
|
}
|
||||||
|
if gotDesc != tt.wantDesc {
|
||||||
|
t.Errorf("parseGeminiLink() gotDesc = %v, want %v", gotDesc, tt.wantDesc)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
17
pkg/gmi2html/templates.go
Normal file
17
pkg/gmi2html/templates.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package gmi2html
|
||||||
|
|
||||||
|
import "html/template"
|
||||||
|
|
||||||
|
// Templates for different line types
|
||||||
|
|
||||||
|
var (
|
||||||
|
textLineTmpl = template.Must(template.New("textLine").Parse(`<p class="gemini-textline">{{.}}</p>`))
|
||||||
|
h1Tmpl = template.Must(template.New("h1").Parse(`<h1 class="gemini-heading-1">{{.}}</h1>`))
|
||||||
|
h2Tmpl = template.Must(template.New("h2").Parse(`<h2 class="gemini-heading-2">{{.}}</h2>`))
|
||||||
|
h3Tmpl = template.Must(template.New("h3").Parse(`<h3 class="gemini-heading-3">{{.}}</h3>`))
|
||||||
|
listItemTmpl = template.Must(template.New("listItem").Parse(`<p class="gemini-list-item">• {{.}}</p>`))
|
||||||
|
blockquoteTmpl = template.Must(template.New("blockquote").Parse(`<blockquote class="gemini-blockquote">{{.}}</blockquote>`))
|
||||||
|
preformattedTmplStart = template.Must(template.New("preformatted").Parse(`<pre class="gemini-preformatted">`))
|
||||||
|
preformattedTmplEnd = template.Must(template.New("preformatted").Parse(`</pre>`))
|
||||||
|
linkTmpl = template.Must(template.New("link").Parse(`<div class="gemini-link-container"><a href="{{.URL}}">{{.Description}}</a></div>`))
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user