-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_html.go
More file actions
73 lines (63 loc) · 2.31 KB
/
Copy pathrender_html.go
File metadata and controls
73 lines (63 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"database/sql"
"fmt"
"os"
"strconv"
"github.com/slzatz/vimango/auth"
)
// DetermineRenderHTML returns the note id passed to --render-html, or -1 if
// the flag is absent. ValidateArgs has already guaranteed the value is
// present and numeric.
func DetermineRenderHTML(args []string) int {
for i, arg := range args {
if arg == "--render-html" && i+1 < len(args) {
if id, err := strconv.Atoi(args[i+1]); err == nil {
return id
}
}
}
return -1
}
// RunRenderHTML renders note <id> as a standalone HTML document on stdout
// and returns a process exit code. Headless: no terminal probes, no vim, no
// raw mode — safe to spawn from a host app with stdout piped (the hybrid
// app's preview pane). Google Drive images resolve to inline data URIs via
// the same auth token and image cache the TUI uses; if Drive is not
// configured the image markdown is passed through untouched.
func RunRenderHTML(id int) int {
app = CreateApp()
prefs := app.LoadPreferences("preferences.json")
app.imageCacheMaxWidth = prefs.ImageCacheMaxWidth
// Optional: without Drive the note still renders, with a visible
// placeholder where each gdrive: image would be. Headless variant —
// stdio is piped, so the interactive OAuth prompt the TUI boot path
// falls into on a missing token.json would block forever here.
if srv, err := auth.GetDriveServiceHeadless(); err == nil {
app.Session.googleDrive = srv
} else {
fmt.Fprintf(os.Stderr, "Warning: Google Drive unavailable: %v\n", err)
}
initImageCache()
if err := app.InitDatabases("config.json", DetermineSQLiteDriver(os.Args)); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not open databases: %v\n", err)
return 1
}
var title string
var note sql.NullString
row := app.Database.MainDB.QueryRow("SELECT title, note FROM task WHERE id=?;", id)
if err := row.Scan(&title, ¬e); err != nil {
fmt.Fprintf(os.Stderr, "Error: no note with id %d: %v\n", id, err)
return 1
}
// Embedded-pane style (standalone=false): no <h1> — the host app
// shows the title in its own native header — and left-aligned
// content instead of a centered reading column.
html, err := RenderNoteAsHTML(title, note.String, false)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: could not render note %d: %v\n", id, err)
return 1
}
fmt.Print(html)
return 0
}