-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement_image.go
163 lines (139 loc) · 3.32 KB
/
element_image.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-FileCopyrightText: 2024 M. Shulhan <[email protected]>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package asciidoctor
import (
"bytes"
"fmt"
"io"
"strings"
"git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
)
// parseBlockImage parse the image block or line.
// The line parameter must not contains the "image::" block or "image:"
// macro prefix.
func (el *element) parseBlockImage(doc *Document, line []byte) bool {
var attrBegin = bytes.IndexByte(line, '[')
if attrBegin < 0 {
return false
}
var attrEnd = bytes.IndexByte(line, ']')
if attrEnd < 0 {
return false
}
var link = bytes.TrimRight(line[:attrBegin], " \t")
link = applySubstitutions(doc, link)
if el.Attrs == nil {
el.Attrs = make(map[string]string)
}
el.Attrs[attrNameSrc] = string(link)
var listAttribute = bytes.Split(line[attrBegin+1:attrEnd], []byte(`,`))
if el.Attrs == nil {
el.Attrs = make(map[string]string)
}
var (
x int
battr []byte
hasWidth bool
)
for x, battr = range listAttribute {
if x == 0 {
var alt = bytes.TrimSpace(listAttribute[0])
if len(alt) == 0 {
var dot = bytes.IndexByte(link, '.')
if dot > 0 {
alt = link[:dot]
}
}
el.Attrs[attrNameAlt] = string(alt)
continue
}
if x == 1 {
if ascii.IsDigits(listAttribute[1]) {
el.Attrs[attrNameWidth] = string(listAttribute[1])
hasWidth = true
continue
}
}
if hasWidth && x == 2 {
if ascii.IsDigits(listAttribute[2]) {
el.Attrs[attrNameHeight] = string(listAttribute[2])
}
}
var attr = string(battr)
var kv = strings.SplitN(attr, `=`, 2)
if len(kv) != 2 {
continue
}
var key = strings.TrimSpace(kv[0])
var val = strings.Trim(kv[1], `"`)
switch key {
case attrNameFloat, attrNameAlign, attrNameRole:
if val == `center` {
val = `text-center`
}
el.addRole(val)
case attrNameLink:
val = string(applySubstitutions(doc, []byte(val)))
el.Attrs[key] = val
default:
el.Attrs[key] = val
}
}
return true
}
func parseInlineImage(doc *Document, content []byte) (el *element, n int) {
// If the next character is ':' (as in block "image::") mark it as
// invalid inline image, since this is block image that has been
// parsed but invalid (probably missing '[]').
if content[0] == ':' {
return nil, 0
}
_, n = indexByteUnescape(content, ']')
if n < 0 {
return nil, 0
}
var lineImage = content[:n+1]
el = &element{
elementAttribute: elementAttribute{
Attrs: make(map[string]string),
},
kind: elKindInlineImage,
}
if el.parseBlockImage(doc, lineImage) {
return el, n + 2
}
return nil, 0
}
func htmlWriteInlineImage(el *element, out io.Writer) {
var (
classes = strings.TrimSpace(`image ` + el.htmlClasses())
link string
withLink bool
)
fmt.Fprintf(out, `<span class=%q>`, classes)
link, withLink = el.Attrs[attrNameLink]
if withLink {
fmt.Fprintf(out, `<a class=%q href=%q>`, attrValueImage, link)
}
var (
src = el.Attrs[attrNameSrc]
alt = el.Attrs[attrNameAlt]
width string
height string
ok bool
)
width, ok = el.Attrs[attrNameWidth]
if ok {
width = fmt.Sprintf(` width="%s"`, width)
}
height, ok = el.Attrs[attrNameHeight]
if ok {
height = fmt.Sprintf(` height="%s"`, height)
}
fmt.Fprintf(out, `<img src=%q alt=%q%s%s>`, src, alt, width, height)
if withLink {
fmt.Fprint(out, `</a>`)
}
fmt.Fprint(out, `</span>`)
}