mirror of
https://github.com/tmedwards/tweego.git
synced 2025-07-07 07:00:35 -04:00
ADD: Add JSON output (-j, --json
).
This commit is contained in:
parent
33a6da381c
commit
3a8dfb2344
8 changed files with 175 additions and 92 deletions
92
passage.go
92
passage.go
|
@ -8,7 +8,6 @@ package main
|
|||
|
||||
import (
|
||||
// standard packages
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
|
@ -35,7 +34,7 @@ var infoPassages = []string{
|
|||
// Compilers: Twine/Twee 1.4+, Twee2, & Tweego.
|
||||
"StorySettings",
|
||||
|
||||
// Compilers: Tweego & (whatever Dan Cox's compiler is called).
|
||||
// Compilers: Tweego, Extwee, & others.
|
||||
"StoryData",
|
||||
|
||||
// Compilers: Twine/Twee 1.4+ & Twee2.
|
||||
|
@ -143,95 +142,6 @@ func (p *passage) isStoryPassage() bool {
|
|||
return !p.hasInfoName() && !p.hasInfoTags()
|
||||
}
|
||||
|
||||
func (p *passage) toTwee(outMode outputMode) string {
|
||||
var output string
|
||||
if outMode == outModeTwee3 {
|
||||
output = ":: " + tweeEscapeString(p.name)
|
||||
if len(p.tags) > 0 {
|
||||
output += " [" + tweeEscapeString(strings.Join(p.tags, " ")) + "]"
|
||||
}
|
||||
if p.hasAnyMetadata() {
|
||||
output += " " + string(p.marshalMetadata())
|
||||
}
|
||||
} else {
|
||||
output = ":: " + p.name
|
||||
if len(p.tags) > 0 {
|
||||
output += " [" + strings.Join(p.tags, " ") + "]"
|
||||
}
|
||||
}
|
||||
output += "\n"
|
||||
if len(p.text) > 0 {
|
||||
output += p.text + "\n"
|
||||
}
|
||||
output += "\n\n"
|
||||
return output
|
||||
}
|
||||
|
||||
func (p *passage) toPassagedata(pid uint) string {
|
||||
var (
|
||||
position string
|
||||
size string
|
||||
)
|
||||
if p.hasMetadataPosition() {
|
||||
position = p.metadata.position
|
||||
} else {
|
||||
// No position metadata, so generate something sensible on the fly.
|
||||
x := pid % 10
|
||||
y := pid / 10
|
||||
if x == 0 {
|
||||
x = 10
|
||||
} else {
|
||||
y++
|
||||
}
|
||||
position = fmt.Sprintf("%d,%d", x*125-25, y*125-25)
|
||||
}
|
||||
if p.hasMetadataSize() {
|
||||
size = p.metadata.size
|
||||
} else {
|
||||
// No size metadata, so default to the normal size.
|
||||
size = "100,100"
|
||||
}
|
||||
|
||||
/*
|
||||
<tw-passagedata pid="…" name="…" tags="…" position="…" size="…">…</tw-passagedata>
|
||||
*/
|
||||
return fmt.Sprintf(`<tw-passagedata pid="%d" name=%q tags=%q position=%q size=%q>%s</tw-passagedata>`,
|
||||
pid,
|
||||
attrEscapeString(p.name),
|
||||
attrEscapeString(strings.Join(p.tags, " ")),
|
||||
attrEscapeString(position),
|
||||
attrEscapeString(size),
|
||||
htmlEscapeString(p.text),
|
||||
)
|
||||
}
|
||||
|
||||
func (p *passage) toTiddler(pid uint) string {
|
||||
var position string
|
||||
if p.hasMetadataPosition() {
|
||||
position = p.metadata.position
|
||||
} else {
|
||||
// No position metadata, so generate something sensible on the fly.
|
||||
x := pid % 10
|
||||
y := pid / 10
|
||||
if x == 0 {
|
||||
x = 10
|
||||
} else {
|
||||
y++
|
||||
}
|
||||
position = fmt.Sprintf("%d,%d", x*140-130, y*140-130)
|
||||
}
|
||||
|
||||
/*
|
||||
<div tiddler="…" tags="…" created="…" modifier="…" twine-position="…">…</div>
|
||||
*/
|
||||
return fmt.Sprintf(`<div tiddler=%q tags=%q twine-position=%q>%s</div>`,
|
||||
attrEscapeString(p.name),
|
||||
attrEscapeString(strings.Join(p.tags, " ")),
|
||||
attrEscapeString(position),
|
||||
tiddlerEscapeString(p.text),
|
||||
)
|
||||
}
|
||||
|
||||
func (p *passage) countWords() uint64 {
|
||||
text := p.text
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue