Commit 30517e5a authored by Ma_124's avatar Ma_124

I/OFFTabSep([]string)

parent 442a56d9
Pipeline #46811411 (#44) failed with stages
in 1 minute and 7 seconds
......@@ -28,10 +28,10 @@ func IFFByName(name string) (*InputFileFormat, error) {
}
case "tabsep":
if len(args) == 1 {
args = []string{"tabesep", "name", "url", "desc"}
return IFFTabSep(NameUrlDescTabSepPattern), nil
} else {
return IFFTabSep(args[1:]), nil
}
// TODO rewrite tabsep
return IFFTabSep(), nil
default:
return nil, fmt.Errorf("%q: unknown iff name", name)
}
......@@ -62,10 +62,10 @@ func OFFByName(name string) (*OutputFileFormat, error) {
return OFFMarkdown(), nil
case "tabsep":
if len(args) == 1 {
args = []string{"tabesep", "name", "url", "desc"}
return OFFTabSep(NameUrlDescTabSepPattern), nil
} else {
return OFFTabSep(args[1:]), nil
}
// TODO rewrite tabsep
return OFFTabSep(), nil
case "html":
if len(args) != 1 {
return nil, fmt.Errorf("%q: wrong number of arguments (usage: html)", name)
......
......@@ -3,9 +3,9 @@ package awesomefw
import (
"github.com/kballard/go-shellquote"
"github.com/stretchr/testify/assert"
"io/ioutil"
"strings"
"testing"
"io/ioutil"
)
func TestIFFByName_Unknown(t *testing.T) {
......@@ -19,7 +19,7 @@ func TestIFFByName_MdListArgs(t *testing.T) {
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b): c\n", OFFTabSep(), NewDebugCtx())
iff.ParseLine(buf, "* [a](b): c\n", OFFTabSep(NameUrlDescTabSepPattern), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
......@@ -29,7 +29,7 @@ func TestIFFByName_MdListDefArgs(t *testing.T) {
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b) - c\n", OFFTabSep(), NewDebugCtx())
iff.ParseLine(buf, "* [a](b) - c\n", OFFTabSep(NameUrlDescTabSepPattern), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
......@@ -51,7 +51,7 @@ func TestIFFByName_TabSepDefArgs(t *testing.T) {
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "a\tb\tc\n", OFFTabSep(), NewDebugCtx())
iff.ParseLine(buf, "a\tb\tc\n", OFFTabSep(NameUrlDescTabSepPattern), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
......@@ -136,7 +136,7 @@ func TestIorOFFByName_ProcessEscEscArgs(t *testing.T) {
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b)\\c\n", OFFTabSep(), NewDebugCtx())
iff.ParseLine(buf, "* [a](b)\\c\n", OFFTabSep(NameUrlDescTabSepPattern), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
......@@ -146,7 +146,7 @@ func TestIFFByName_ProcessEscAtArgs(t *testing.T) {
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b)@c\n", OFFTabSep(), NewDebugCtx())
iff.ParseLine(buf, "* [a](b)@c\n", OFFTabSep(NameUrlDescTabSepPattern), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
......@@ -164,7 +164,7 @@ func TestIFFByName_ProcessAtArgs(t *testing.T) {
assert.Equal(t, nil, err)
buf := &strings.Builder{}
iff.ParseLine(buf, "* [a](b)||c\n", OFFTabSep(), NewDebugCtx())
iff.ParseLine(buf, "* [a](b)||c\n", OFFTabSep(NameUrlDescTabSepPattern), NewDebugCtx())
assert.Equal(t, "a\tb\tc\n", buf.String())
}
......
......@@ -280,7 +280,7 @@ func mainCompile() {
func getInFFByName(name string) *awesomefw.InputFileFormat {
switch strings.ToLower(name) {
case "tabsep":
return awesomefw.IFFTabSep()
return awesomefw.IFFTabSep(awesomefw.NameUrlDescTabSepPattern)
case "md":
fallthrough
case "markdown":
......
......@@ -32,7 +32,7 @@ func TestCompile_TabSepTabSepInvalid(t *testing.T) {
defer func() {
r := recover()
assert.Equal(t, "invalid line: \"ghi\"", r)
assert.Equal(t, "\"ghi\": expected <url>\n", r)
}()
in := `
......@@ -41,7 +41,7 @@ d e f
ghi
`[1:]
Compile(strings.NewReader(in), len(in), &strings.Builder{}, IFFTabSep(), OFFTabSep(), nil)
Compile(strings.NewReader(in), len(in), &strings.Builder{}, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), nil)
}
func TestCompile_MarkdownTabSep(t *testing.T) {
......@@ -61,7 +61,7 @@ A curated list of awesome curated lists of many topics.
For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issues) and/or a [pull request](https://github.com/emijrp/awesome-awesome/pulls). Above there are some ideas for missing lists, be bold!
`[1:]
Compile(strings.NewReader(in), len(in), out, IFFMarkdown(" - "), OFFTabSep(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(in), len(in), out, IFFMarkdown(" - "), OFFTabSep(NameUrlDescTabSepPattern), &ApisCfg{&ApiCfg{}})
assert.Equal(t, awesomeAwesomeShortOut, out.String())
}
......@@ -78,7 +78,7 @@ func TestCompileFile_TmpFile(t *testing.T) {
f.Close()
buf := &strings.Builder{}
CompileFile(f.Name(), buf, IFFTabSep(), OFFTabSep(), &ApisCfg{&ApiCfg{}})
CompileFile(f.Name(), buf, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), &ApisCfg{&ApiCfg{}})
assert.Equal(t, buf.String(), awesomeAwesomeShortOut)
}
......@@ -93,7 +93,7 @@ func TestCompileFile_UnknownFile(t *testing.T) {
}
}()
CompileFile("/unknown", nil, IFFTabSep(), OFFTabSep(), nil)
CompileFile("/unknown", nil, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), nil)
}
func TestCompileFile_NoReadPermFile(t *testing.T) {
......@@ -113,7 +113,7 @@ func TestCompileFile_NoReadPermFile(t *testing.T) {
panic(err)
}
CompileFile(fname, &strings.Builder{}, IFFTabSep(), OFFTabSep(), nil)
CompileFile(fname, &strings.Builder{}, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), nil)
}
type testCompileFileReadErr struct{}
......@@ -129,5 +129,5 @@ func TestCompileFile_ReadErr(t *testing.T) {
assert.Equal(t, r, errors.New("error by mocked reader"))
}()
Compile(&testCompileFileReadErr{}, 0, &strings.Builder{}, IFFTabSep(), OFFTabSep(), nil)
Compile(&testCompileFileReadErr{}, 0, &strings.Builder{}, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), nil)
}
......@@ -20,16 +20,16 @@ A curated list of awesome curated lists of many topics.
"## Computer management
"
awesome-scalability https://github.com/binhnguyennus/awesome-scalability An up-to-date and curated reading list for designing high scalability, high availability, high stabilityback-end systems.
awesome-shell https://github.com/alebcay/awesome-shell Command-line frameworks, toolkits, guides and gizmos.
awesome-sysadmin https://github.com/kahun/awesome-sysadmin Backups, configuration management, DNS, IMAP/POP3, LDAP, monitoring, SSH, statistics, troubleshooting, virtualization, VPN and more.
awesome-scalability https://github.com/binhnguyennus/awesome-scalability An up-to-date and curated reading list for designing high scalability, high availability, high stabilityback-end systems.
awesome-shell https://github.com/alebcay/awesome-shell Command-line frameworks, toolkits, guides and gizmos.
awesome-sysadmin https://github.com/kahun/awesome-sysadmin Backups, configuration management, DNS, IMAP/POP3, LDAP, monitoring, SSH, statistics, troubleshooting, virtualization, VPN and more.
"
"For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issues) and/or a [pull request](https://github.com/emijrp/awesome-awesome/pulls). Above there are some ideas for missing lists, be bold!
`[1:]
func TestCompile_TabSepTabSep(t *testing.T) {
out := &strings.Builder{}
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(), OFFTabSep(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(NameUrlDescTabSepPattern), OFFTabSep(NameUrlDescTabSepPattern), &ApisCfg{&ApiCfg{}})
assert.Equal(t, `
'# Awesome
......@@ -51,7 +51,7 @@ awesome-sysadmin https://github.com/kahun/awesome-sysadmin Backups, configuratio
func TestCompile_TabSepSimpleMarkdown(t *testing.T) {
out := &strings.Builder{}
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(), OFFSimpleMarkdown(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(NameUrlDescTabSepPattern), OFFSimpleMarkdown(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, `
# Awesome Awesome
......@@ -75,7 +75,7 @@ For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issu
func TestCompile_TabSepMarkdown(t *testing.T) {
out := &strings.Builder{}
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(), OFFMarkdown(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(NameUrlDescTabSepPattern), OFFMarkdown(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, `
# Awesome Awesome
......@@ -99,7 +99,7 @@ For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issu
func TestCompile_TabSepSimpleMarkdownHtml(t *testing.T) {
out := &strings.Builder{}
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(), OFFSimpleMarkdownHtml(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(NameUrlDescTabSepPattern), OFFSimpleMarkdownHtml(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, string(blackfriday.Run([]byte(`
# Awesome Awesome
......@@ -123,7 +123,7 @@ For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issu
func TestCompile_TabSepMarkdownHtml(t *testing.T) {
out := &strings.Builder{}
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(), OFFMarkdownHtml(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(awesomeAwesomeShortInp), len(awesomeAwesomeShortInp), out, IFFTabSep(NameUrlDescTabSepPattern), OFFMarkdownHtml(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, string(blackfriday.Run([]byte(`
# Awesome Awesome
......@@ -156,7 +156,7 @@ g h i
j k l
"</main>`[1:]
Compile(strings.NewReader(in), len(in), out, IFFTabSep(), OFFHtml(), &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(in), len(in), out, IFFTabSep(NameUrlDescTabSepPattern), OFFHtml(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, `
<main>
......
......@@ -3,9 +3,13 @@ package awesomefw
import (
"fmt"
"io"
"regexp"
"strings"
)
var NameUrlDescTabSepPattern = []string{"name", "url", "desc"}
var tabsepRegexp = regexp.MustCompile(`(\s{3}|\t)\s*`)
// Parses the code according to this pattern:
// <name>\t<url>\t<desc>
// "literal line
......@@ -16,52 +20,70 @@ import (
// which spans over
// multiple lines
// """
func IFFTabSep() *InputFileFormat {
return &InputFileFormat{parseTabSepLine}
func IFFTabSep(fields []string) *InputFileFormat {
return &InputFileFormat{parseTabSepLine(fields)}
}
func parseTabSepLine(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
if ctx.InFFCtx["tabsep--in-literal"] == nil {
ctx.InFFCtx["tabsep--in-literal"] = false
}
func parseTabSepLine(fieldDefs []string) func(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
return func(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
if ctx.InFFCtx["tabsep--in-literal"] == nil {
ctx.InFFCtx["tabsep--in-literal"] = false
}
if l == "\"\"\"\n" || l == "\"\"\"\r\n" { // Start literal block
ctx.InFFCtx["tabsep--in-literal"] = !ctx.InFFCtx["tabsep--in-literal"].(bool)
return
}
if l == "\"\"\"\n" || l == "\"\"\"\r\n" { // Start literal block
ctx.InFFCtx["tabsep--in-literal"] = !ctx.InFFCtx["tabsep--in-literal"].(bool)
return
}
if ctx.InFFCtx["tabsep--in-literal"].(bool) {
outFF.Literal(out, l, ctx)
return
}
if ctx.InFFCtx["tabsep--in-literal"].(bool) {
outFF.Literal(out, l, ctx)
return
}
if len(l) == 1 {
return
}
if len(l) == 1 {
return
}
if l[0] == '"' { // Start line literal
outFF.Literal(out, l[1:], ctx)
return
}
if l[0] == '"' { // Start line literal
outFF.Literal(out, l[1:], ctx)
return
}
if l[0] == '\'' { // Start line literal without newline
outFF.Literal(out, strings.TrimLeft(l[1:len(l)-1], "\r\n"), ctx)
return
}
if l[0] == '\'' { // Start line literal without newline
outFF.Literal(out, strings.TrimLeft(l[1:len(l)-1], "\r\n"), ctx)
return
}
l = l[:len(l)-1]
l = l[:len(l)-1]
lps := strings.SplitN(l, "\t", 3)
if len(lps) < 3 {
panic(fmt.Sprintf("invalid line: %q", l))
}
fields := tabsepRegexp.Split(l, -1)
it := &Item{}
i := 0
outFF.Item(out, &Item{
lps[0],
lps[1],
"",
lps[2],
}, ctx)
for _, fieldDef := range fieldDefs {
if i >= len(fields) {
panic(fmt.Sprintf("%q: expected <%s>\n", l, fieldDef))
}
switch fieldDef {
case "name":
it.Name = fields[i]
case "url":
it.Url = fields[i]
case "repo-url":
it.RepoUrl = fields[i]
case "desc":
it.Desc = fields[i]
default:
panic(fmt.Sprintf("%q: unknown fieldDef (%q)", l, fieldDef))
}
i++
}
outFF.Item(out, it, ctx)
}
}
// Prints the code according to this pattern:
......@@ -72,15 +94,15 @@ func parseTabSepLine(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx)
// "block literal
// "which spans over
// "multiple lines
func OFFTabSep() *OutputFileFormat {
func OFFTabSep(fields []string) *OutputFileFormat {
return &OutputFileFormat{
writeLiteralTabSep,
writeItemTabSep,
writeItemTabSep(fields),
nil, nil,
}
}
func writeLiteralTabSep(out io.Writer, l string, ctx *Ctx) {
func writeLiteralTabSep(out io.Writer, l string, _ *Ctx) {
// TODO literal blocks
// TODO align (once multitab support is added)
// TODO multiple `'`s to `"`
......@@ -94,11 +116,27 @@ func writeLiteralTabSep(out io.Writer, l string, ctx *Ctx) {
}
}
func writeItemTabSep(out io.Writer, item *Item, ctx *Ctx) {
out.Write([]byte(item.Name))
out.Write([]byte("\t"))
out.Write([]byte(item.Url))
out.Write([]byte("\t"))
out.Write([]byte(item.Desc))
out.Write([]byte("\n"))
func writeItemTabSep(fieldDefs []string) func(out io.Writer, it *Item, _ *Ctx) {
return func(out io.Writer, it *Item, _ *Ctx) {
for i, fieldDef := range fieldDefs {
switch fieldDef {
case "name":
out.Write([]byte(it.Name))
case "url":
out.Write([]byte(it.Url))
case "repo-url":
out.Write([]byte(it.RepoUrl))
case "desc":
out.Write([]byte(it.Desc))
default:
panic(fmt.Sprintf("%v: unknown fieldDef (%q)", it, fieldDef))
}
if i < len(fieldDefs)-1 {
out.Write([]byte("\t"))
}
}
out.Write([]byte("\n"))
}
}
......@@ -134,7 +134,7 @@ func testTestOFF(t *testing.T, name string, off *OutputFileFormat, fail bool, in
panic(r)
}
}()
Compile(strings.NewReader(in), len(in), ioutil.Discard, IFFTabSep(), off, &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(in), len(in), ioutil.Discard, IFFTabSep(NameUrlDescTabSepPattern), off, &ApisCfg{&ApiCfg{}})
}
func TestNoopLiteral(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment