Commit 79718e3b authored by Ma_124's avatar Ma_124

added O/IFFByName and converted to func() *O/IFF

parent c209fd30
Pipeline #46599560 failed with stages
in 1 minute and 3 seconds
cmd/awesome/_test/outlist.md
.hooks/**
TODO.txt
TODO.md
......@@ -117,10 +117,8 @@ multiple lines
Data Sources:
* https://github.com/example/repo
* http://github.com/example/repo
* https://godoc.org/github.com/example/repo
* http://godoc.org/github.com/example/repo
* https://gitlab.com/example/repo
* http://gitlab.com/example/repo
......
package awesomefw
import (
"strings"
"github.com/kballard/go-shellquote"
"errors"
"fmt"
"io/ioutil"
)
func IFFByName(name string) (*InputFileFormat, error) {
args, err := shellquote.Split(strings.TrimSpace(name))
if err != nil {
return nil, err
}
switch args[0] {
case "md:list":
if len(args) == 1 {
return IFFMarkdown(" - "), nil
} else if len(args) == 2 {
return IFFMarkdown(args[1]), nil
} else {
return nil, fmt.Errorf("%q: wrong number of arguments (usage: md [sep])", name)
}
case "tabsep":
if len(args) == 1 {
args = []string{"tabesep", "name", "url", "desc"}
}
// TODO rewrite tabsep
return IFFTabSep(), nil
default:
return nil, errors.New("unknown iff name")
}
}
func OFFByName(name string) (*OutputFileFormat, error) {
args, err := shellquote.Split(strings.TrimSpace(name))
if err != nil {
return nil, err
}
for i := range args {
if args[i][0] == '\\' || args[i][1] == '\\' {
args[i] = args[i][1:]
} else if args[i][0] == '\\' || args[i][1] == '@' {
args[i] = args[i][1:]
} else if args[i][0] == '@' {
s, err := ioutil.ReadFile(args[i][1:])
if err != nil {
return nil, fmt.Errorf("%q: cannot open file %q", name, args[i][1:])
}
args[i] = string(s)
}
}
// TODO md>html
switch args[0] {
case "md:table":
if len(args) != 1 {
return nil, fmt.Errorf("%q: wrong number of arguments (usage: md:table)", name)
}
return OFFSimpleMarkdown(), nil
case "md:table+meta":
if len(args) != 1 {
return nil, fmt.Errorf("%q: wrong number of arguments (usage: md:table+meta)", name)
}
return OFFMarkdown(), nil
case "tabsep":
if len(args) == 1 {
args = []string{"tabesep", "name", "url", "desc"}
}
// TODO rewrite tabsep
return OFFTabSep(), nil
case "html":
if len(args) != 1 {
return nil, fmt.Errorf("%q: wrong number of arguments (usage: html)", name)
}
return OFFHtml(), nil
default:
return nil, errors.New("unknown off name")
}
}
......@@ -280,7 +280,7 @@ func mainCompile() {
func getInFFByName(name string) *awesomefw.InputFileFormat {
switch strings.ToLower(name) {
case "tabsep":
return awesomefw.IFFTabSep
return awesomefw.IFFTabSep()
case "md":
fallthrough
case "markdown":
......@@ -288,7 +288,7 @@ func getInFFByName(name string) *awesomefw.InputFileFormat {
case "md-simple":
fallthrough
case "markdown-simple":
return awesomefw.IFFMarkdown
return awesomefw.IFFMarkdown(" - ")
}
return nil
......@@ -299,21 +299,21 @@ func getOutFFByName(name string) *awesomefw.OutputFileFormat {
case "md-simple-html":
fallthrough
case "markdown-simple-html":
return awesomefw.OFFSimpleMarkdownHtml
return awesomefw.OFFSimpleMarkdownHtml()
case "md-html":
fallthrough
case "markdown-html":
return awesomefw.OFFMarkdownHtml
return awesomefw.OFFMarkdownHtml()
case "md":
fallthrough
case "markdown":
return awesomefw.OFFMarkdown
return awesomefw.OFFMarkdown()
case "tabsep":
return awesomefw.OFFTabSep
return awesomefw.OFFTabSep()
case "md-simple":
fallthrough
case "markdown-simple":
return awesomefw.OFFSimpleMarkdown
return awesomefw.OFFSimpleMarkdown()
}
return nil
......
......@@ -37,7 +37,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(), OFFTabSep(), nil)
}
func TestCompile_MarkdownTabSep(t *testing.T) {
......@@ -57,7 +57,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(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, awesomeAwesomeShortOut, out.String())
}
......@@ -74,7 +74,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(), OFFTabSep(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, buf.String(), awesomeAwesomeShortOut)
}
......@@ -89,7 +89,7 @@ func TestCompileFile_UnknownFile(t *testing.T) {
}
}()
CompileFile("/unknown", nil, IFFTabSep, OFFTabSep, nil)
CompileFile("/unknown", nil, IFFTabSep(), OFFTabSep(), nil)
}
type testCompileFileReadErr struct{}
......@@ -105,5 +105,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(), OFFTabSep(), nil)
}
......@@ -29,7 +29,7 @@ awesome-sysadmin https://github.com/kahun/awesome-sysadmin Backups, configuratio
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(), OFFTabSep(), &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(), 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(), 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(), 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(), OFFMarkdownHtml(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, string(blackfriday.Run([]byte(`
# Awesome Awesome
......@@ -148,30 +148,18 @@ For contributing, [open an issue](https://github.com/emijrp/awesome-awesome/issu
func TestCompile_TabSepHtml(t *testing.T) {
out := &strings.Builder{}
in := `
"<html>
" <head>
" <meta charset="UTF-8">
" </head>
" <body>
" <main>
"<main>
a b c
d e f
"<h2>Section 2</h2>
g h i
j k l
" </main>
" </body>
"</html>`[1:]
"</main>`[1:]
Compile(strings.NewReader(in), len(in), out, IFFTabSep, OFFHtml, &ApisCfg{&ApiCfg{}})
Compile(strings.NewReader(in), len(in), out, IFFTabSep(), OFFHtml(), &ApisCfg{&ApiCfg{}})
assert.Equal(t, `
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<main>
<main>
<table><thead><tr><th>Stars</th><th>Forks</th><th>Issues</th><th>Last Commit</th><th>Name</th><th>Desc</th></tr></thead><tbody>
<tr><td>N/A</td><td>N/A</td><td>N/A</td><td>N/A</td><td><a href="b">a</a></td><td>c</td></tr>
<tr><td>N/A</td><td>N/A</td><td>N/A</td><td>N/A</td><td><a href="e">d</a></td><td>f</td></tr>
......@@ -181,8 +169,6 @@ j k l
<tr><td>N/A</td><td>N/A</td><td>N/A</td><td>N/A</td><td><a href="h">g</a></td><td>i</td></tr>
<tr><td>N/A</td><td>N/A</td><td>N/A</td><td>N/A</td><td><a href="k">j</a></td><td>l</td></tr>
</tbody></table>
</main>
</body>
</html>
</main>
`[1:], out.String())
}
......@@ -2,7 +2,14 @@ package awesomefw
import "io"
var OFFHtml = &OutputFileFormat{ writeLiteralHtml, writeItemHtml, startHtml, nil }
func OFFHtml() *OutputFileFormat {
return &OutputFileFormat{
writeLiteralHtml,
writeItemHtml,
startHtml,
nil,
}
}
func startHtml(out io.Writer, ctx *Ctx) {
ctx.OutFFCtx["writeTblHead"] = true
......
......@@ -8,18 +8,25 @@ import (
// Parses the code according to this pattern:
// * [<name>](<url>) - <desc>
var IFFMarkdown = &InputFileFormat{parseSimpleBulletMarkdownLine}
func parseSimpleBulletMarkdownLine(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
starIndx := strings.IndexByte(l, '*')
if starIndx != -1 && (l[0] == ' ' || l[0] == '*') && strings.HasPrefix(l[starIndx:], "* [") {
afterNameI := strings.IndexByte(l, ']')
outFF.Item(out,
l[starIndx+3:afterNameI],
l[afterNameI+2:strings.IndexByte(l[afterNameI+2:], ')')+afterNameI+2],
l[strings.Index(l, " - ")+3:len(l)-1], ctx)
} else {
outFF.Literal(out, l, ctx)
// sep: " - "
func IFFMarkdown(sep string) *InputFileFormat {
return &InputFileFormat{
parseSimpleBulletMarkdownLine(sep),
}
}
func parseSimpleBulletMarkdownLine(sep string) func(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
return func(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
starIndx := strings.IndexByte(l, '*')
if starIndx != -1 && (l[0] == ' ' || l[0] == '*') && strings.HasPrefix(l[starIndx:], "* [") {
afterNameI := strings.IndexByte(l, ']')
outFF.Item(out,
l[starIndx+3:afterNameI],
l[afterNameI+2:strings.IndexByte(l[afterNameI+2:], ')')+afterNameI+2],
l[strings.Index(l, sep)+3:len(l)-1], ctx)
} else {
outFF.Literal(out, l, ctx)
}
}
}
......@@ -27,13 +34,25 @@ func parseSimpleBulletMarkdownLine(out io.Writer, l string, outFF *OutputFileFor
// | Name | Desc |
// |-----------------|--------|
// | [<name>](<url>) | <desc> |
var OFFSimpleMarkdown = &OutputFileFormat{writeLiteralMd, writeSimpleItemMd, nil, nil}
func OFFSimpleMarkdown() *OutputFileFormat {
return &OutputFileFormat{
writeLiteralMd,
writeSimpleItemMd,
nil, nil,
}
}
// Prints the code according to this pattern:
// | Stars | Forks | Issues | Last Commit | Name | Desc |
// |---------|---------|----------|---------------|-----------------|--------|
// | <stars> | <forks> | <issues> | <last commit> | [<name>](<url>) | <desc> |
var OFFMarkdown = &OutputFileFormat{writeLiteralMd, writeItemMd, nil, nil}
func OFFMarkdown() *OutputFileFormat {
return &OutputFileFormat{
writeLiteralMd,
writeItemMd,
nil, nil,
}
}
func writeLiteralMd(out io.Writer, l string, ctx *Ctx) {
ctx.OutFFCtx["writeTblHead"] = true
......@@ -83,10 +102,23 @@ func writeItemMd(out io.Writer, name, url, desc string, ctx *Ctx) {
}
// Same as OFFSimpleMarkdown but compiled to HTML by gopkg.in/russross/blackfriday.v2
var OFFSimpleMarkdownHtml = &OutputFileFormat{writeLiteralMdHtml, writeSimpleItemMdHtml, startMdHtml, endMdHtml}
func OFFSimpleMarkdownHtml() *OutputFileFormat {
return &OutputFileFormat{
writeLiteralMdHtml,
writeSimpleItemMdHtml,
startMdHtml,
endMdHtml,
}
}
// Same as OFFMarkdown but compiled to HTML by gopkg.in/russross/blackfriday.v2
var OFFMarkdownHtml = &OutputFileFormat{writeLiteralMdHtml, writeItemMdHtml, startMdHtml, endMdHtml}
func OFFMarkdownHtml() *OutputFileFormat {
return &OutputFileFormat{
writeLiteralMdHtml,
writeItemMdHtml,
startMdHtml, endMdHtml,
}
}
func startMdHtml(out io.Writer, ctx *Ctx) {
ctx.OutFFCtx["md-html--writer"] = &strings.Builder{}
......
......@@ -16,7 +16,9 @@ import (
// which spans over
// multiple lines
// """
var IFFTabSep = &InputFileFormat{parseTabSepLine}
func IFFTabSep() *InputFileFormat {
return &InputFileFormat{parseTabSepLine}
}
func parseTabSepLine(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx) {
if ctx.InFFCtx["tabsep--in-literal"] == nil {
......@@ -65,7 +67,13 @@ func parseTabSepLine(out io.Writer, l string, outFF *OutputFileFormat, ctx *Ctx)
// "block literal
// "which spans over
// "multiple lines
var OFFTabSep = &OutputFileFormat{writeLiteralTabSep, writeItemTabSep, nil, nil}
func OFFTabSep() *OutputFileFormat {
return &OutputFileFormat{
writeLiteralTabSep,
writeItemTabSep,
nil, nil,
}
}
func writeLiteralTabSep(out io.Writer, l string, ctx *Ctx) {
// TODO literal blocks
......
......@@ -9,7 +9,14 @@ import (
*/
// Panics if the items aren't alphabetically sorted. Nothing is written to out
var OFFTestAlphabetical = &OutputFileFormat{noopLiteral, testAlpha, startTestAlpha, nil}
func OFFTestAlphabetical() *OutputFileFormat {
return &OutputFileFormat{
noopLiteral,
testAlpha,
startTestAlpha,
nil,
}
}
func noopLiteral(_ io.Writer, _ string, _ *Ctx) {}
......
......@@ -7,7 +7,7 @@ import (
)
func TestCompile_TestAlphaSorted(t *testing.T) {
testTestOFF(t, "alpha_sorted", OFFTestAlphabetical, false, `
testTestOFF(t, "alpha_sorted", OFFTestAlphabetical(), false, `
A Z 6
B D 3
D B 8
......@@ -16,7 +16,7 @@ Z A 0
}
func TestCompile_TestAlphaUnsorted(t *testing.T) {
testTestOFF(t, "alpha_unsorted", OFFTestAlphabetical, true, `
testTestOFF(t, "alpha_unsorted", OFFTestAlphabetical(), true, `
A Z 6
D B 8
B D 3
......@@ -25,7 +25,7 @@ Z A 0
}
func TestCompile_TestAlphaDuplicate(t *testing.T) {
testTestOFF(t, "alpha_dup", OFFTestAlphabetical, true, `
testTestOFF(t, "alpha_dup", OFFTestAlphabetical(), true, `
A Z 6
D B 8
D Y 5
......@@ -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(), 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