-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package markup | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var imgRe = regexp.MustCompile(`^img\s+{`) | ||
|
||
func MatchesImg(line string) bool { | ||
return imgRe.MatchString(line) | ||
} | ||
|
||
type imgEntry struct { | ||
path string | ||
sizeH string | ||
sizeV string | ||
desc string | ||
} | ||
|
||
type Img struct { | ||
entries []imgEntry | ||
inDesc bool | ||
hyphaName string | ||
} | ||
|
||
func (img *Img) Process(line string) (shouldGoBackToNormal bool) { | ||
if img.inDesc { | ||
rightBraceIndex := strings.IndexRune(line, '}') | ||
if cnt := len(img.entries); rightBraceIndex == -1 && cnt != 0 { | ||
img.entries[cnt-1].desc += "\n" + line | ||
} else if rightBraceIndex != -1 && cnt != 0 { | ||
img.entries[cnt-1].desc += "\n" + line[:rightBraceIndex] | ||
img.inDesc = false | ||
} | ||
if strings.Count(line, "}") > 1 { | ||
return true | ||
} | ||
} else if s := strings.TrimSpace(line); s != "" { | ||
if s[0] == '}' { | ||
return true | ||
} | ||
img.parseStartOfEntry(line) | ||
} | ||
return false | ||
} | ||
|
||
func ImgFromFirstLine(line, hyphaName string) Img { | ||
img := Img{ | ||
hyphaName: hyphaName, | ||
entries: make([]imgEntry, 0), | ||
} | ||
line = line[strings.IndexRune(line, '{'):] | ||
if len(line) == 1 { // if { only | ||
} else { | ||
line = line[1:] // Drop the { | ||
} | ||
return img | ||
} | ||
|
||
func (img *Img) canonicalPathFor(path string) string { | ||
path = strings.TrimSpace(path) | ||
if strings.IndexRune(path, ':') != -1 || strings.IndexRune(path, '/') == 0 { | ||
return path | ||
} else { | ||
return "/binary/" + xclCanonicalName(img.hyphaName, path) | ||
} | ||
} | ||
|
||
func (img *Img) parseStartOfEntry(line string) (entry imgEntry, followedByDesc bool) { | ||
pipeIndex := strings.IndexRune(line, '|') | ||
if pipeIndex == -1 { // If no : in string | ||
entry.path = img.canonicalPathFor(line) | ||
} else { | ||
entry.path = img.canonicalPathFor(line[:pipeIndex]) | ||
line = strings.TrimPrefix(line, line[:pipeIndex+1]) | ||
|
||
var ( | ||
leftBraceIndex = strings.IndexRune(line, '{') | ||
rightBraceIndex = strings.IndexRune(line, '}') | ||
dimensions string | ||
) | ||
|
||
if leftBraceIndex == -1 { | ||
dimensions = line | ||
} else { | ||
dimensions = line[:leftBraceIndex] | ||
} | ||
|
||
sizeH, sizeV := parseDimensions(dimensions) | ||
entry.sizeH = sizeH | ||
entry.sizeV = sizeV | ||
|
||
if leftBraceIndex != -1 && rightBraceIndex == -1 { | ||
img.inDesc = true | ||
followedByDesc = true | ||
entry.desc = strings.TrimPrefix(line, line[:leftBraceIndex+1]) | ||
} else if leftBraceIndex != -1 && rightBraceIndex != -1 { | ||
entry.desc = line[leftBraceIndex+1 : rightBraceIndex] | ||
} | ||
} | ||
img.entries = append(img.entries, entry) | ||
return | ||
} | ||
|
||
func parseDimensions(dimensions string) (sizeH, sizeV string) { | ||
xIndex := strings.IndexRune(dimensions, '*') | ||
if xIndex == -1 { // If no x in dimensions | ||
sizeH = strings.TrimSpace(dimensions) | ||
} else { | ||
sizeH = strings.TrimSpace(dimensions[:xIndex]) | ||
sizeV = strings.TrimSpace(strings.TrimPrefix(dimensions, dimensions[:xIndex+1])) | ||
} | ||
return | ||
} | ||
|
||
func (img Img) ToHtml() (html string) { | ||
for _, entry := range img.entries { | ||
html += fmt.Sprintf(`<figure> | ||
<img src="%s" width="%s" height="%s"> | ||
`, entry.path, entry.sizeH, entry.sizeV) | ||
if entry.desc != "" { | ||
html += ` <figcaption>` | ||
for i, line := range strings.Split(entry.desc, "\n") { | ||
if line != "" { | ||
if i > 0 { | ||
html += `<br>` | ||
} | ||
html += ParagraphToHtml(line) | ||
} | ||
} | ||
html += `</figcaption>` | ||
} | ||
html += `</figure>` | ||
} | ||
return `<section class="img-gallery"> | ||
` + html + ` | ||
</section>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package markup | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestParseStartOfEntry(t *testing.T) { | ||
img := ImgFromFirstLine("img {", "h") | ||
tests := []struct { | ||
line string | ||
entry imgEntry | ||
followedByDesc bool | ||
}{ | ||
{"apple", imgEntry{"apple", "", "", ""}, false}, | ||
{"pear:", imgEntry{"pear", "", "", ""}, false}, | ||
{"яблоко: 30*60", imgEntry{"яблоко", "30", "60", ""}, false}, | ||
{"груша : 65 ", imgEntry{"груша", "65", "", ""}, false}, | ||
{"жеронимо : 30 { full desc }", imgEntry{"жеронимо", "30", "", " full desc "}, false}, | ||
{"жорно жованна : *5555 {partial description", imgEntry{"жорно_жованна", "", "5555", "partial description"}, true}, | ||
{"иноске : {full}", imgEntry{"иноске", "", "", "full"}, false}, | ||
{"j:{partial", imgEntry{"j", "", "", "partial"}, true}, | ||
} | ||
for _, triplet := range tests { | ||
entry, followedByDesc := img.parseStartOfEntry(triplet.line) | ||
if entry != triplet.entry || followedByDesc != triplet.followedByDesc { | ||
t.Error(fmt.Sprintf("%q:%q != %q; %v != %v", triplet.line, entry, triplet.entry, followedByDesc, triplet.followedByDesc)) | ||
} | ||
} | ||
} | ||
|
||
func TestParseDimensions(t *testing.T) { | ||
tests := [][]string{ | ||
{"500", "500", ""}, | ||
{"3em", "3em", ""}, | ||
{"500*", "500", ""}, | ||
{"*500", "", "500"}, | ||
{"800*520", "800", "520"}, | ||
{"17%*5rem", "17%", "5rem"}, | ||
} | ||
for _, triplet := range tests { | ||
sizeH, sizeV := parseDimensions(triplet[0]) | ||
if sizeH != triplet[1] || sizeV != triplet[2] { | ||
t.Error(sizeH, "*", sizeV, " != ", triplet[1], "*", triplet[2]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.