-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tl8-new, refactor common code into internal/tl8
- Loading branch information
1 parent
6f6b1c6
commit 2d56c27
Showing
5 changed files
with
268 additions
and
146 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"translation/internal/tl8" | ||
) | ||
|
||
func tl8new1(fn string) error { | ||
source, err := ioutil.ReadFile(fn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
doc, err := tl8.Segment(source) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
lines := strings.Split(string(source), "\n") | ||
for idx, heading := range doc.Headings { | ||
if idx == 0 { | ||
continue // skip document title heading | ||
} | ||
line := lines[heading.Line-1] | ||
if strings.Contains(line, "translated=") { | ||
return fmt.Errorf("document already contains translated= markers") | ||
} | ||
line = strings.Replace(line, "}", ` translated="TODO"}`, 1) | ||
lines[heading.Line-1] = line | ||
} | ||
|
||
return ioutil.WriteFile(fn, []byte(strings.Join(lines, "\n")), 0644) | ||
} | ||
|
||
func tl8new() error { | ||
flag.Parse() | ||
if flag.NArg() != 1 { | ||
return fmt.Errorf("syntax: %s <markdown-file>", filepath.Base(os.Args[0])) | ||
} | ||
|
||
return tl8new1(flag.Arg(0)) | ||
} | ||
|
||
func main() { | ||
if err := tl8new(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,56 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
source := []byte(`# document {version="4_18"} | ||
Introduction. | ||
## first heading {#first} | ||
Old explanation. | ||
## second heading {#second} | ||
Unchanged explanation. | ||
`) | ||
tmp := t.TempDir() | ||
fn := filepath.Join(tmp, "userguide.markdown") | ||
if err := ioutil.WriteFile(fn, source, 0644); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := tl8new1(fn); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
wantSource := []byte(`# document {version="4_18"} | ||
Introduction. | ||
## first heading {#first translated="TODO"} | ||
Old explanation. | ||
## second heading {#second translated="TODO"} | ||
Unchanged explanation. | ||
`) | ||
|
||
updatedSource, err := ioutil.ReadFile(fn) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := cmp.Diff(wantSource, updatedSource); diff != "" { | ||
t.Errorf("unexpected new translation update: diff (-want +got):\n%s", diff) | ||
} | ||
|
||
} |
Oops, something went wrong.