Skip to content

Commit

Permalink
Merge pull request #3 from yushi/adjust-header-for-wide-character
Browse files Browse the repository at this point in the history
Adjust header for wide characters.
  • Loading branch information
dmitshur committed May 23, 2014
2 parents 7a6ce01 + 813654f commit 83c9a6a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"Comment": "null-125",
"Rev": "88fa3094706609347d6fa1e362d946bbc207fceb"
},
{
"ImportPath": "github.com/mattn/go-runewidth",
"Comment": "travisish-12-g39104c7",
"Rev": "39104c78546f11be4bbc161e39858258aac6d9b8"
},
{
"ImportPath": "github.com/russross/blackfriday",
"Comment": "v1.1-115-gf3ab184",
Expand Down
7 changes: 5 additions & 2 deletions markdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strings"

"github.com/mattn/go-runewidth"
"github.com/russross/blackfriday"
)

Expand Down Expand Up @@ -100,9 +101,11 @@ func (_ *markdownRenderer) Header(out *bytes.Buffer, text func() bool, level int

switch level {
case 1:
fmt.Fprint(out, "\n", strings.Repeat("=", out.Len()-textMarker))
len := runewidth.StringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("=", len))
case 2:
fmt.Fprint(out, "\n", strings.Repeat("-", out.Len()-textMarker))
len := runewidth.StringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("-", len))
}
out.WriteString("\n")
}
Expand Down
36 changes: 36 additions & 0 deletions markdown/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ func Test(t *testing.T) {
}
}

func TestWideChar(t *testing.T) {
input := []byte(`タイトル
==
サブタイトル
---
aaa/あああ
----------
`)

expected := []byte(`タイトル
========
サブタイトル
------------
aaa/あああ
----------
`)

output, err := markdown.Process("", input, nil)
if err != nil {
log.Fatalln(err)
}

diff, err := diff(expected, output)
if err != nil {
log.Fatalln(err)
}

if len(diff) != 0 {
t.Errorf("Difference of %d lines:\n%s", bytes.Count(diff, []byte("\n")), string(diff))
}
}

// TODO: Factor out.
func diff(b1, b2 []byte) (data []byte, err error) {
f1, err := ioutil.TempFile("", "markdownfmt")
Expand Down

0 comments on commit 83c9a6a

Please sign in to comment.