forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
57 lines (46 loc) · 1.42 KB
/
plugin.go
File metadata and controls
57 lines (46 loc) · 1.42 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
package main
import (
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"strings"
"github.com/JohannesKaufmann/dom"
"github.com/fatih/color"
"github.com/qeesung/image2ascii/convert"
"github.com/romance-dev/browser/converter"
"golang.org/x/net/html"
)
// Transform <img>
func renderImg(terminalWidth int, downloadImages bool) converter.HandleRenderFunc {
return func(ctx converter.Context, w converter.Writer, node *html.Node) converter.RenderStatus {
/*
<img alt="alt text" src="/image.png"> => 
*/
if src, ok := dom.GetAttribute(node, "src"); ok {
imageData, err := downloadImage(src, terminalWidth)
if err != nil {
if alt, ok := dom.GetAttribute(node, "alt"); ok {
w.WriteString(fmt.Sprintf("[Image: %s]", alt))
}
return converter.RenderSuccess
}
var sb strings.Builder
sb.WriteString("\n```\n")
convertOptions := convert.DefaultOptions
converter := convert.NewImageConverter()
convertOptions.FitScreen = true
convertOptions.Colored = true
convertOptions.Reversed = true
sb.WriteString(converter.Image2ASCIIString(imageData, &convertOptions))
if alt, ok := dom.GetAttribute(node, "alt"); ok && alt != "" {
fmt.Fprint(&sb, "\n")
color.New(color.Underline, color.FgBlack, color.Bold).Fprintln(&sb, alt)
}
sb.WriteString("```\n")
// Display image instead
w.WriteString(sb.String())
}
return converter.RenderSuccess
}
}