Compare commits

9 Commits

Author SHA1 Message Date
antanst
139b2e7733 . 2025-10-15 09:03:50 +03:00
antanst
971f1e5206 Add a gmi test file. 2025-06-03 12:40:36 +03:00
antanst
39651a6021 Fix rendering bugs, update name 2025-06-03 12:38:53 +03:00
82a2083422 . 2025-03-21 10:47:15 +02:00
af3bdd7d9a Import Source Serif Pro font from Google Fonts
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 10:57:58 +02:00
d3c36c9e74 Add --replace-gmi-ext parameter for .gmi to .html link conversion
This commit adds a new boolean parameter to convert .gmi links to .html in the output:
- Adds a new parseGeminiLink parameter to replace .gmi with .html in links
- Updates the CLI tool to expose --replace-gmi-ext flag
- Adds tests for the new functionality
- Updates documentation

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-10 16:51:56 +02:00
8126ded0a6 Add contentOnly parameter. 2025-03-06 11:48:02 +02:00
25c39036d3 Additional gccgo Makefile target 2025-03-02 15:00:10 +02:00
f78a89c56c Return errors properly from gmi2html 2025-03-02 15:00:01 +02:00
13 changed files with 628 additions and 305 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/.idea
/.vscode
/dist

44
AGENTS.md Normal file
View File

@@ -0,0 +1,44 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Essential Commands
Build, test, and develop:
```shell
make # Full workflow: format, lint, test, clean, build
make test # Run tests only
make build # Build binary to ./dist/gmi2html
make fmt # Format code with gofumpt and gci
make lint # Run linter after formatting
make lintfix # Run linter with auto-fix
```
Running the tool:
```shell
./dist/gmi2html <input.gmi >output.html
./dist/gmi2html --no-container <input.gmi >content.html
./dist/gmi2html --replace-gmi-ext <input.gmi >output.html
```
## Architecture
This is a Go library and CLI tool that converts Gemini text format to HTML.
**Core Components:**
- `gmi2html.go`: Main conversion logic with `Gmi2html()` function and `convertGeminiContent()` for parsing
- `templates.go`: HTML templates for each Gemini element type (headings, links, lists, etc.)
- `cmd/gmi2html/main.go`: CLI entry point that reads from stdin and writes to stdout
- `assets/main.html`: Embedded HTML template with CSS for the full page container
**Key Architecture Patterns:**
- Uses Go's `html/template` package for safe HTML generation with automatic escaping
- Embeds the main HTML template using `//go:embed` directive
- Line-by-line parser that switches between normal and preformatted modes
- Two output modes: full HTML document or content-only for embedding
- Optional `.gmi` to `.html` link conversion for static site generation
**Gemini Format Support:**
- Headings (#, ##, ###), links (=>), lists (*), quotes (>), preformatted blocks (```)
- Proper handling of preformatted content with mode switching
- URL parsing and validation for links

98
CLAUDE.md Normal file
View File

@@ -0,0 +1,98 @@
# CLAUDE.md
This file provides guidance to AI Agents such as Claude Code or ChatGPT Codex when working with code in this repository.
## General guidelines
Use idiomatic Go as possible. Prefer simple code than complex.
## Project Overview
Gemserve is a simple Gemini protocol server written in Go that serves static files over TLS-encrypted connections. The Gemini protocol is a lightweight, privacy-focused alternative to HTTP designed for serving text-based content.
### Development Commands
```bash
# Build, test, and format everything
make
# Run tests only
make test
# Build binaries to ./dist/ (gemserve, gemget, gembench)
make build
# Format code with gofumpt and gci
make fmt
# Run golangci-lint
make lint
# Run linter with auto-fix
make lintfix
# Clean build artifacts
make clean
# Run the server (after building)
./dist/gemserve
# Generate TLS certificates for development
certs/generate.sh
```
### Architecture
Core Components
- **cmd/gemserve/gemserve.go**: Entry point with TLS server setup, signal handling, and graceful shutdown
- **cmd/gemget/**: Gemini protocol client for fetching content
- **cmd/gembench/**: Benchmarking tool for Gemini servers
- **server/**: Request processing, file serving, and Gemini protocol response handling
- **gemini/**: Gemini protocol implementation (URL parsing, status codes, path normalization)
- **config/**: CLI-based configuration system
- **lib/logging/**: Structured logging package with context-aware loggers
- **lib/apperrors/**: Application error handling (fatal vs non-fatal errors)
- **uid/**: Connection ID generation for logging (uses external vendor package)
Key Patterns
- **Security First**: All file operations use `filepath.IsLocal()` and path cleaning to prevent directory traversal
- **Error Handling**: Uses structured errors via `lib/apperrors` package distinguishing fatal from non-fatal errors
- **Logging**: Structured logging with configurable levels via internal logging package
- **Testing**: Table-driven tests with parallel execution, heavy focus on security edge cases
Request Flow
1. TLS connection established on port 1965
2. Read up to 1KB request (Gemini spec limit)
3. Parse and normalize Gemini URL
4. Validate path security (prevent traversal)
5. Serve file or directory index with appropriate MIME type
6. Send response with proper Gemini status codes
Configuration
Server configured via CLI flags:
- `--listen`: Server address (default: localhost:1965)
- `--root-path`: Directory to serve files from
- `--dir-indexing`: Enable directory browsing (default: false)
- `--log-level`: Logging verbosity (debug, info, warn, error; default: info)
- `--response-timeout`: Response timeout in seconds (default: 30)
- `--tls-cert`: TLS certificate file path (default: certs/server.crt)
- `--tls-key`: TLS key file path (default: certs/server.key)
- `--max-response-size`: Maximum response size in bytes (default: 5242880)
Testing Strategy
- **server/server_test.go**: Path security and file serving tests
- **gemini/url_test.go**: URL parsing and normalization tests
- Focus on security edge cases (Unicode, traversal attempts, malformed URLs)
- Use parallel test execution for performance
Security Considerations
- All connections require TLS certificates (stored in certs/)
- Path traversal protection is critical - test thoroughly when modifying file serving logic
- Request size limited to 1KB per Gemini specification
- Input validation on all URLs and paths

View File

@@ -1,4 +1,4 @@
SHELL := /bin/env oksh SHELL := /bin/sh
export PATH := $(PATH) export PATH := $(PATH)
all: fmt lintfix tidy test clean build all: fmt lintfix tidy test clean build
@@ -8,7 +8,6 @@ debug:
@echo "GOPATH: $(shell go env GOPATH)" @echo "GOPATH: $(shell go env GOPATH)"
@which go @which go
@which gofumpt @which gofumpt
@which gci
@which golangci-lint @which golangci-lint
clean: clean:
@@ -16,7 +15,7 @@ clean:
# Test # Test
test: test:
go test -race ./... go test ./...
tidy: tidy:
go mod tidy go mod tidy
@@ -24,7 +23,6 @@ tidy:
# Format code # Format code
fmt: fmt:
gofumpt -l -w . gofumpt -l -w .
gci write .
# Run linter # Run linter
lint: fmt lint: fmt
@@ -36,7 +34,10 @@ lintfix: fmt
build: clean build: clean
mkdir ./dist mkdir ./dist
go build -race -o ./dist/gmi2html ./bin/gmi2html/gmi2html.go go build -o ./dist/gmi2html ./cmd/gmi2html
build-gccgo: clean
go build -compiler=gccgo -o ./dist/gmi2html ./cmd/gmi2html
show-updates: show-updates:
go list -m -u all go list -m -u all

View File

@@ -13,3 +13,23 @@ 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
```

130
assets/main.html Normal file
View File

@@ -0,0 +1,130 @@
<!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="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,200..900;1,200..900&family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&display=swap" rel="stylesheet">
<style>
:root {
--text-color: #333;
--bg-color: #ffffff;
--link-color: #0066cc;
--link-hover: #0052a3;
--quote-bg: #f5f5f5;
--quote-border: #ddd;
--pre-bg: #f8f8f8;
--pre-border: #e1e1e1;
}
@media (prefers-color-scheme: dark) {
:root {
--text-color: #e0e0e0;
--bg-color: #1a1a1a;
--link-color: #66a3ff;
--link-hover: #87b5ff;
--quote-bg: #2a2a2a;
--quote-border: #444;
--pre-bg: #252525;
--pre-border: #3a3a3a;
}
}
body {
font-family: serif;
/* font-weight: 300; */
font-size: 16px;
color: var(--text-color);
background-color: var(--bg-color);
max-width: 34rem;
amargin: 0 auto;
amargin-left: 2rem;
padding: 1rem 1rem;
atext-align: justify;
ahyphens: auto;
a-webkit-hyphens: auto;
a-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>

View File

@@ -1,31 +0,0 @@
package main
import (
"fmt"
"io"
"os"
"github.com/antanst/gmi2html"
)
func main() {
err := runApp()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runApp() error {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
html := gmi2html.Gmi2html(string(data), "")
_, err = fmt.Fprintf(os.Stdout, "%s", html)
if err != nil {
return err
}
return nil
}

38
cmd/gmi2html/main.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"flag"
"fmt"
"io"
"os"
"git.antanst.com/antanst/gmi2html"
)
func main() {
noContainer := flag.Bool("no-container", false, "Don't output container HTML")
replaceGmiExt := flag.Bool("replace-gmi-ext", false, "In links, replace original .gmi extension with .html")
flag.Parse()
err := runApp(*noContainer, *replaceGmiExt)
if err != nil {
fmt.Fprintln(os.Stderr, 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
}

View File

@@ -2,6 +2,7 @@ package gmi2html
import ( import (
"bytes" "bytes"
_ "embed"
"fmt" "fmt"
"html/template" "html/template"
"net/url" "net/url"
@@ -11,13 +12,21 @@ 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 //go:embed assets/main.html
func Gmi2html(text string, title string) string { var rawTemplate string
content := convertGeminiContent(text)
// 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))
// Handle any template errors with container
var buffer bytes.Buffer var buffer bytes.Buffer
err := containerTmpl.Execute(&buffer, struct { err := tmpl.Execute(&buffer, struct {
Title string Title string
Content template.HTML Content template.HTML
}{ }{
@@ -26,25 +35,48 @@ func Gmi2html(text string, title string) string {
}) })
if err != nil { if err != nil {
fmt.Printf("Error executing container template: %s\n", err) fmt.Printf("Error executing container template: %s\n", err)
return "" return "", err
} }
return buffer.String() return buffer.String(), nil
} }
// convertGeminiContent converts Gemini text to HTML with proper escaping // convertGeminiContent converts Gemini text to HTML with proper escaping
func convertGeminiContent(text string) string { func convertGeminiContent(text string, replaceGmiExt bool) string {
lines := strings.Split(text, "\n") lines := strings.Split(text, "\n")
var buffer bytes.Buffer var buffer bytes.Buffer
normalMode := true normalMode := true
for _, line := range lines { for _, line := range lines {
if 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 itself
continue
}
if !normalMode {
// Inside preformatted block - output line directly with HTML escaping
buffer.WriteString(template.HTMLEscapeString(line))
buffer.WriteString("\n")
continue
}
// Normal mode - process gemini markup
switch { switch {
case strings.HasPrefix(line, "=>"): case strings.HasPrefix(line, "=>"):
handleLinkLine(&buffer, line) handleLinkLine(&buffer, line, replaceGmiExt)
case strings.HasPrefix(line, "```"):
normalMode = !normalMode
// Don't output the ``` line
case strings.HasPrefix(line, "###"): case strings.HasPrefix(line, "###"):
content := strings.TrimSpace(strings.TrimPrefix(line, "###")) content := strings.TrimSpace(strings.TrimPrefix(line, "###"))
err := h3Tmpl.Execute(&buffer, content) err := h3Tmpl.Execute(&buffer, content)
@@ -76,17 +108,10 @@ func convertGeminiContent(text string) string {
return "" return ""
} }
default: default:
if normalMode {
err := textLineTmpl.Execute(&buffer, line) err := textLineTmpl.Execute(&buffer, line)
if err != nil { if err != nil {
return "" return ""
} }
} else {
err := preformattedTmpl.Execute(&buffer, line)
if err != nil {
return ""
}
}
} }
} }
@@ -94,8 +119,8 @@ func convertGeminiContent(text string) string {
} }
// handleLinkLine parses and renders a link line // handleLinkLine parses and renders a link line
func handleLinkLine(buffer *bytes.Buffer, linkLine string) { func handleLinkLine(buffer *bytes.Buffer, linkLine string, replaceGmiExt bool) {
url, description, err := parseGeminiLink(linkLine) url, description, err := parseGeminiLink(linkLine, replaceGmiExt)
if err != nil { if err != nil {
fmt.Printf("Error parsing gemini link line: %s\n", err) fmt.Printf("Error parsing gemini link line: %s\n", err)
return return
@@ -110,7 +135,7 @@ func handleLinkLine(buffer *bytes.Buffer, linkLine string) {
} }
// parseGeminiLink extracts URL and description from a link line // parseGeminiLink extracts URL and description from a link line
func parseGeminiLink(linkLine string) (string, string, error) { func parseGeminiLink(linkLine string, replaceGmiExt bool) (string, string, error) {
re := regexp.MustCompile(`^=>[ \t]+(\S+)([ \t]+.*)?`) re := regexp.MustCompile(`^=>[ \t]+(\S+)([ \t]+.*)?`)
matches := re.FindStringSubmatch(linkLine) matches := re.FindStringSubmatch(linkLine)
if len(matches) == 0 { if len(matches) == 0 {
@@ -125,6 +150,11 @@ func parseGeminiLink(linkLine string) (string, string, error) {
return "", "", fmt.Errorf("error parsing link line: %w input '%s'", err, linkLine) 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 // Set description to URL if not provided
description := urlStr description := urlStr
if len(matches) > 2 && strings.TrimSpace(matches[2]) != "" { if len(matches) > 2 && strings.TrimSpace(matches[2]) != "" {

View File

@@ -1,158 +1,178 @@
package gmi2html package gmi2html
import ( import (
"strings"
"testing" "testing"
) )
func TestConvertGeminiContent(t *testing.T) { func TestGmi2html(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input string input string
expected string title string
contentOnly bool
replaceGmiExt bool
want string
wantErr bool
}{ }{
{ {
name: "Simple text line", name: "Basic text",
input: "This is a simple text line", input: "Hello world",
expected: `<p class="gemini-textline">This is a simple text line</p>`, title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<p class=\"gemini-textline\">Hello world</p>",
wantErr: false,
}, },
{ {
name: "Heading line level 1", name: "Headers",
input: "# Main heading", input: "# Header 1\n## Header 2\n### Header 3",
expected: `<h1 class="gemini-heading-1">Main heading</h1>`, 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: "Link line with description", name: "List items",
input: "=> https://example.com Example site", input: "* Item 1\n* Item 2",
expected: `<div class="gemini-link-container"><a href="https://example.com">Example site</a></div>`, 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: "List item", name: "Blockquote",
input: "* List item 1",
expected: `<p class="gemini-list-item">• List item 1</p>`,
},
{
name: "Quote line",
input: "> This is a quote", input: "> This is a quote",
expected: `<blockquote class="gemini-blockquote">This is a quote</blockquote>`, 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", name: "Preformatted text",
input: "```\ncode line 1\ncode line 2\n```", input: "```\nThis is preformatted\n```",
expected: `<pre class="gemini-preformatted">code line 1</pre><pre class="gemini-preformatted">code line 2</pre>`, title: "Test",
contentOnly: true,
replaceGmiExt: false,
want: "<pre class=\"gemini-preformatted\">\nThis is preformatted\n</pre>",
wantErr: false,
}, },
{ {
name: "Mixed content", name: "Mixed content",
input: "# Title\n\nNormal paragraph\n\n=> https://example.com Link to example", input: "# Title\nNormal text\n=> https://example.com Link\n```\nCode\n```\n* List item",
expected: `<h1 class="gemini-heading-1">Title</h1><p class="gemini-textline"></p><p class="gemini-textline">Normal paragraph</p><p class="gemini-textline"></p><div class="gemini-link-container"><a href="https://example.com">Link to example</a></div>`, 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
result := convertGeminiContent(tt.input) got, err := Gmi2html(tt.input, tt.title, tt.contentOnly, tt.replaceGmiExt)
if result != tt.expected { if (err != nil) != tt.wantErr {
t.Errorf("convertGeminiContent(%q):\ngot: %s\nwant: %s", t.Errorf("Gmi2html() error = %v, wantErr %v", err, tt.wantErr)
tt.input, result, tt.expected) return
}
if got != tt.want {
t.Errorf("Gmi2html() = %v, want %v", got, tt.want)
} }
}) })
} }
} }
func TestGmi2html(t *testing.T) {
sample := "# Hello Gemini\n\nThis is a test document.\n\n=> https://gemini.circumlunar.space/ Project Gemini"
result := Gmi2html(sample, "Gemini Test")
// Check that it contains the expected elements
if !strings.Contains(result, "<title>Gemini Test</title>") {
t.Error("Output HTML missing title")
}
if !strings.Contains(result, "<h1 class=\"gemini-heading-1\">Hello Gemini</h1>") {
t.Error("Output HTML missing properly formatted heading")
}
if !strings.Contains(result, "<a href=\"https://gemini.circumlunar.space/\">Project Gemini</a>") {
t.Error("Output HTML missing properly formatted link")
}
// Check that CSS is included
if !strings.Contains(result, "<style>") {
t.Error("Output HTML missing style section")
}
// Check that basic HTML structure is there
if !strings.Contains(result, "<!DOCTYPE html>") {
t.Error("Output HTML missing doctype declaration")
}
if !strings.Contains(result, "<div class=\"gemini-container\">") {
t.Error("Output HTML missing container div")
}
}
func TestParseGeminiLink(t *testing.T) { func TestParseGeminiLink(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input string linkLine string
expectedURL string replaceGmiExt bool
expectedDesc string wantURL string
expectError bool wantDesc string
wantErr bool
}{ }{
{ {
name: "Valid link with description", name: "Basic link",
input: "=> https://example.com Example site", linkLine: "=> https://example.com Example Link",
expectedURL: "https://example.com", replaceGmiExt: false,
expectedDesc: "Example site", wantURL: "https://example.com",
expectError: false, wantDesc: "Example Link",
wantErr: false,
}, },
{ {
name: "Valid link without description", name: "Link without description",
input: "=> https://example.com", linkLine: "=> https://example.com",
expectedURL: "https://example.com", replaceGmiExt: false,
expectedDesc: "https://example.com", wantURL: "https://example.com",
expectError: false, wantDesc: "https://example.com",
wantErr: false,
}, },
{ {
name: "Link with special characters", name: "Invalid link format",
input: "=> https://example.com/search?q=test Test search", linkLine: "Invalid line",
expectedURL: "https://example.com/search?q=test", replaceGmiExt: false,
expectedDesc: "Test search", wantURL: "",
expectError: false, wantDesc: "",
wantErr: true,
}, },
{ {
name: "Malformed link line", name: "Link with .gmi extension, no replacement",
input: "=>", linkLine: "=> /path/file.gmi Link to Gemini file",
expectedURL: "", replaceGmiExt: false,
expectedDesc: "", wantURL: "/path/file.gmi",
expectError: true, wantDesc: "Link to Gemini file",
wantErr: false,
}, },
{ {
name: "Link with multiple spaces in description", name: "Link with .gmi extension, with replacement",
input: "=> https://example.com Multiple spaces", linkLine: "=> /path/file.gmi Link to Gemini file",
expectedURL: "https://example.com", replaceGmiExt: true,
expectedDesc: "Multiple spaces", wantURL: "/path/file.html",
expectError: false, 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
url, desc, err := parseGeminiLink(tt.input) gotURL, gotDesc, err := parseGeminiLink(tt.linkLine, tt.replaceGmiExt)
if (err != nil) != tt.wantErr {
// Check error expectation t.Errorf("parseGeminiLink() error = %v, wantErr %v", err, tt.wantErr)
if (err != nil) != tt.expectError { return
t.Errorf("Expected error: %v, got error: %v", tt.expectError, err != nil)
} }
if gotURL != tt.wantURL {
// If we expect success, check the result t.Errorf("parseGeminiLink() gotURL = %v, want %v", gotURL, tt.wantURL)
if !tt.expectError {
if url != tt.expectedURL {
t.Errorf("Expected URL: %s, got: %s", tt.expectedURL, url)
}
if desc != tt.expectedDesc {
t.Errorf("Expected description: %s, got: %s", tt.expectedDesc, desc)
} }
if gotDesc != tt.wantDesc {
t.Errorf("parseGeminiLink() gotDesc = %v, want %v", gotDesc, tt.wantDesc)
} }
}) })
} }

4
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/antanst/gmi2html module git.antanst.com/antanst/gmi2html
go 1.23.6 go 1.21

View File

@@ -11,133 +11,7 @@ var (
h3Tmpl = template.Must(template.New("h3").Parse(`<h3 class="gemini-heading-3">{{.}}</h3>`)) 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>`)) 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>`)) blockquoteTmpl = template.Must(template.New("blockquote").Parse(`<blockquote class="gemini-blockquote">{{.}}</blockquote>`))
preformattedTmpl = template.Must(template.New("preformatted").Parse(`<pre class="gemini-preformatted">{{.}}</pre>`)) 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>`)) linkTmpl = template.Must(template.New("link").Parse(`<div class="gemini-link-container"><a href="{{.URL}}">{{.Description}}</a></div>`))
// Container template with typography-focused CSS
containerTmpl = template.Must(template.New("container").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
<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;
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>`))
) )

96
test_files/1.gmi Normal file
View File

@@ -0,0 +1,96 @@
# pollux.casa 🟊.⌂
```ascii-art pollux.casa
_______ _______ ___ ___ __ __ __ __ ___
| | | | | | | | | | |_| | / \
| _ | _ | | | | | | | | | / \
| |_| | | | | | | | | |_| | | / _ _ \
| ___| |_| | |___| |___| || | ___ ||_| |_||
| | | | | | | _ | | | | _ |
|___| |_______|_______|_______|_______|__| |__| |___| |__|_|__|
```
=> /index.fr.gmi 🏳️ Version française
## A gemini capsules free hosting
You want to create a gemini capsule, but do not want to manage a server?
You're welcome !
This service is proposed and managed by Adële
=> gemini://adele.pollux.casa/ Adële's capsule
If your "pseudo" is still available, your capsule will be created and reachable on this address:
```
gemini://"pseudo".pollux.casa/
```
Also accessible in http/https if you want to share with persons that do not know gemini yet:
```
https://"pseudo".pollux.casa/
```
Go on board, it will 🚀
=> requestaccount.gmi How to request an account
=> content.gmi How to edit your gemini capsule content
## News
=> /gemlog/ news gemlog
## Why pollux.casa domain name?
* 🟊 Pollux, because it is one of main stars in Gemini constellation
* ⌂ Casa, because it will become your home in this constellation
## Hardware & software
pollux.casa is hosted on a small Intel NUC machine
* Processor Celeron N2830 @2.16GHz (64bits)
* 4 Gb RAM
* Disk SATA 1 Tb (5400rpm)
* ADSL connection 6Mb/s up and 23Mb/s down
* OS Archlinux
* Gemserv Gemini server & Lighttpd + Ergol-http
=> img/nuc.jpg pollux.casa server !
## Charter and policy
This service is offered freely without service guarantee. However, this service will be supplied in accordance to the Charter of CHATONS.
CHATONS is a collective of independant, transparent, open, neutral and ethical hosters providing FLOSS-based online services.
=> https://www.chatons.org/en/manifeste Manifesto of CHATONS
=> https://www.chatons.org/en/charte Charter of CHATONS
pollux.casa is not an official entity so, it cannot officially be member of CHATONS.
I'm French and lives in France, so French laws will be applied to this service.
## Free to leave pollux.casa
If you decide to self-host your capsule, it is possible download your capsule (of course, you have uploaded it) and to redirect "pseudo.pollux.casa" to your own domain and machine. I'll be happy to have helped you to start gemini experience.
## Contact
You can join me on the Fediverse:
=> https://social.pollux.casa/@adele @adele@social.pollux.casa
Or by email:
=> /adele_at_pollux.casa.pub.asc get openpgp pub key of adele(at)pollux.casa
``` asci-art astronaut
_____
./ \. _
/ .-""-\ _/ \
.-| /:. | | |
| \ |:. /.-'-./
| .-'-;:__.' =/
.'= *=| _.='
/ _. | 🚀 ;
;-.-'| \ |
/ | \ _\ _\
\__/'._;. ==' ==\
\ \ |
/ / /
/-._/-._/
\ `\ \
`-._/._/
```
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Time: 2.569 ms