Exemple #1
0
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))
	}
}
Exemple #2
0
func Example2() {
	input := []byte(`Title
==

Subtitle
---

How about ` + "`this`" + ` and other stuff like *italic*, **bold** and ***super    extra***.
`)

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

	os.Stdout.Write(output)

	// Output:
	//Title
	//=====
	//
	//Subtitle
	//--------
	//
	//How about `this` and other stuff like *italic*, **bold** and ***super extra***.
	//
}
Exemple #3
0
func ProcessMarkdown(text string) string {
	output, err := markdown.Process("", []byte(text), nil)
	if err != nil {
		println("ProcessMarkdown:", err.Error())
		return text
	}
	return string(output)
}
Exemple #4
0
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error {
	if in == nil {
		f, err := os.Open(filename)
		if err != nil {
			return err
		}
		defer f.Close()
		in = f
	}

	src, err := ioutil.ReadAll(in)
	if err != nil {
		return err
	}

	stdout := int(os.Stdout.Fd())
	res, err := markdown.Process(filename, src, &markdown.Options{
		Terminal: !*write && terminal.IsTerminal(stdout),
	})
	if err != nil {
		return err
	}

	if !bytes.Equal(src, res) {
		// formatting has changed
		if *list {
			fmt.Fprintln(out, filename)
		}
		if *write {
			err = ioutil.WriteFile(filename, res, 0)
			if err != nil {
				return err
			}
		}
		if *doDiff {
			data, err := diff(src, res)
			if err != nil {
				return fmt.Errorf("computing diff: %s", err)
			}
			fmt.Printf("diff %s markdownfmt/%s\n", filename, filename)
			out.Write(data)
		}
	}

	if !*list && !*write && !*doDiff {
		_, err = out.Write(res)
	}

	return err
}
Exemple #5
0
func Test(t *testing.T) {
	output, err := markdown.Process("", []byte(reference), nil)
	if err != nil {
		log.Fatalln(err)
	}

	diff, err := diff([]byte(reference), 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))
	}
}
Exemple #6
0
func TestLineBreak(t *testing.T) {
	input := []byte("Some text with two trailing spaces for linebreak.  \nMore      spaced      **text**      *immediately*      after      that.         \nMore than two spaces become two.\n")
	expected := []byte("Some text with two trailing spaces for linebreak.  \nMore spaced **text** *immediately* after that.  \nMore than two spaces become two.\n")

	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))
	}
}
Exemple #7
0
func Example() {
	input := []byte(`Title
=

This is a new paragraph. I wonder    if I have too     many spaces.
What about new paragraph.
But the next one...

  Is really new.

1. Item one.
1. Item TWO.


Final paragraph.
`)

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

	os.Stdout.Write(output)

	// Output:
	//Title
	//=====
	//
	//This is a new paragraph. I wonder if I have too many spaces. What about new paragraph. But the next one...
	//
	//Is really new.
	//
	//1.	Item one.
	//2.	Item TWO.
	//
	//Final paragraph.
	//
}
Exemple #8
0
func processFile(filename string, in io.ReadSeeker, out io.Writer, stdin bool) error {
	if in == nil {
		f, err := os.Open(filename)
		if err != nil {
			return err
		}
		defer f.Close()
		in = f
	}

	// slurp in the whole file for comparison later
	src, err := ioutil.ReadAll(in)
	if err != nil {
		return err
	}
	in.Seek(0, 0)

	// parse the file with hugo/parser to extract front matter
	page, err := parser.ReadFrom(in)
	if err != nil {
		return err
	}

	md, err := markdown.Process(filename, page.Content(), nil)
	if err != nil {
		return err
	}

	// If we have front matter, insert a newline to separate the front matter
	// from the markdown content.
	sep := ""
	if len(page.FrontMatter()) > 0 {
		sep = "\n"
	}

	res := make([]byte, len(page.FrontMatter())+len(sep)+len(md))
	copy(res, append(append(page.FrontMatter(), []byte(sep)...), md...))

	if !bytes.Equal(src, res) {
		// formatting has changed
		if *list {
			fmt.Fprintln(out, filename)
		}
		if *write {
			err = ioutil.WriteFile(filename, res, 0)
			if err != nil {
				return err
			}
		}
		if *doDiff {
			data, err := diff(src, res)
			if err != nil {
				return fmt.Errorf("computing diff: %s", err)
			}
			fmt.Printf("diff %s mdfmt/%s\n", filename, filename)
			out.Write(data)
		}
	}

	if !*list && !*write && !*doDiff {
		_, err = out.Write(res)
	}

	return err
}
Exemple #9
0
// https://github.com/shurcooL/markdownfmt/issues/20
func TestSuccessiveLines(t *testing.T) {
	input := []byte(`text
text

[link](https://github.com)
text

*italic*
text

**bold**
text

***massive***
text

` + "`" + `noformat` + "`" + `
text

text
[link](https://github.com)

text
*italic*

text
**bold**

text
***massive***

text
` + "`" + `noformat` + "`" + `
`)
	expected := []byte(`text text

[link](https://github.com) text

*italic* text

**bold** text

***massive*** text

` + "`" + `noformat` + "`" + ` text

text [link](https://github.com)

text *italic*

text **bold**

text ***massive***

text ` + "`" + `noformat` + "`" + `
`)

	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))
	}
}