Skip to content

Commit

Permalink
Merge pull request #1525 from nhooyr/utf16
Browse files Browse the repository at this point in the history
d2parser: Support reading utf16 files
  • Loading branch information
nhooyr authored Aug 2, 2023
2 parents 62a48bc + 2039537 commit 8269390
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 54 deletions.
10 changes: 5 additions & 5 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ import (
)

type CompileOptions struct {
UTF16 bool
UTF16Pos bool
// FS is the file system used for resolving imports in the d2 text.
// It should correspond to the root path.
FS fs.FS
}

func Compile(p string, r io.RuneReader, opts *CompileOptions) (*d2graph.Graph, *d2target.Config, error) {
func Compile(p string, r io.Reader, opts *CompileOptions) (*d2graph.Graph, *d2target.Config, error) {
if opts == nil {
opts = &CompileOptions{}
}

ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{
UTF16: opts.UTF16,
UTF16Pos: opts.UTF16Pos,
})
if err != nil {
return nil, nil, err
}

ir, err := d2ir.Compile(ast, &d2ir.CompileOptions{
UTF16: opts.UTF16,
FS: opts.FS,
UTF16Pos: opts.UTF16Pos,
FS: opts.FS,
})
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion d2exporter/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func run(t *testing.T, tc testCase) {
ctx = log.Leveled(ctx, slog.LevelDebug)

g, config, err := d2compiler.Compile("", strings.NewReader(tc.dsl), &d2compiler.CompileOptions{
UTF16: true,
UTF16Pos: true,
})
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type compiler struct {
importStack []string
// importCache enables reuse of files imported multiple times.
importCache map[string]*Map
utf16 bool
utf16Pos bool

globStack []bool
}

type CompileOptions struct {
UTF16 bool
UTF16Pos bool
// Pass nil to disable imports.
FS fs.FS
}
Expand All @@ -45,7 +45,7 @@ func Compile(ast *d2ast.Map, opts *CompileOptions) (*Map, error) {
fs: opts.FS,

importCache: make(map[string]*Map),
utf16: opts.UTF16,
utf16Pos: opts.UTF16Pos,
}
m := &Map{}
m.initRoot()
Expand Down
5 changes: 2 additions & 3 deletions d2ir/import.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package d2ir

import (
"bufio"
"io/fs"
"os"
"path"
Expand Down Expand Up @@ -99,8 +98,8 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
}
defer f.Close()

ast, err := d2parser.Parse(impPath, bufio.NewReader(f), &d2parser.ParseOptions{
UTF16: c.utf16,
ast, err := d2parser.Parse(impPath, f, &d2parser.ParseOptions{
UTF16Pos: c.utf16Pos,
ParseError: c.err,
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions d2lib/d2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type CompileOptions struct {
UTF16 bool
UTF16Pos bool
FS fs.FS
MeasuredTexts []*d2target.MText
Ruler *textmeasure.Ruler
Expand All @@ -50,8 +50,8 @@ func Compile(ctx context.Context, input string, compileOpts *CompileOptions, ren
}

g, config, err := d2compiler.Compile(compileOpts.InputPath, strings.NewReader(input), &d2compiler.CompileOptions{
UTF16: compileOpts.UTF16,
FS: compileOpts.FS,
UTF16Pos: compileOpts.UTF16Pos,
FS: compileOpts.FS,
})
if err != nil {
return nil, nil, err
Expand Down
1 change: 1 addition & 0 deletions d2parser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
utf16.d2
Loading

0 comments on commit 8269390

Please sign in to comment.