Skip to content

Commit

Permalink
Change to the way of writing using receiver.
Browse files Browse the repository at this point in the history
  • Loading branch information
kentaro-m committed May 4, 2017
1 parent 0d9e515 commit e9be560
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
md2confl is a CLI tool to convert the markdown text to confluence wiki format.

## Demo
![](./demo.gif)

## Install
```
Expand All @@ -18,7 +19,8 @@ Usage:
Available Commands:
copy Copy a confluence wiki text converted from markdown.
preview Shows a confluence wiki text converted from markdown.
preview Show a confluence wiki text converted from markdown.
version Print the version number of md2confl
help Help about any command
Flags:
Expand Down
18 changes: 10 additions & 8 deletions cmd/copy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/atotto/clipboard"
"github.com/kentaro-m/md2confl/util"
"github.com/kentaro-m/md2confl/utils/util"
"github.com/kentaro-m/md2confl/utils/file"
"github.com/kentaro-m/md2confl/confluence"
)

Expand All @@ -17,10 +15,14 @@ var copyCmd = &cobra.Command{
}

func copy(cmd *cobra.Command, args []string) error {
data := util.ReadFile(args[0])
output := confluence.Convert(data)
fmt.Println("Copied to clipboard!")
return clipboard.WriteAll(output)
file := file.File{}
if err := file.Open(args[0]); err != nil {
return err
}

confluence := confluence.Confluence{}
confluence.Convert(file.Data)
return util.Copy(confluence.Contents)
}

func init() {
Expand Down
20 changes: 12 additions & 8 deletions cmd/preview.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/kentaro-m/md2confl/util"
"github.com/kentaro-m/md2confl/confluence"
"github.com/kentaro-m/md2confl/utils/file"
)

var previewCmd = &cobra.Command{
Use: "preview [file path]",
Short: "Shows a confluence wiki text converted from markdown.",
Long: "Shows a confluence wiki text converted from markdown.",
Short: "Show a confluence wiki text converted from markdown.",
Long: "Show a confluence wiki text converted from markdown.",
RunE: preview,
}

func preview(cmd *cobra.Command, args []string) error {
data := util.ReadFile(args[0])
output := confluence.Convert(data)
fmt.Println(output)
file := file.File{}
if err := file.Open(args[0]); err != nil {
return err
}

confluence := confluence.Confluence{}
confluence.Convert(file.Data)
confluence.Preview()

return nil
}

Expand Down
17 changes: 15 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/spf13/cobra"
)

const (
VERSION = "0.1.0"
)

var RootCmd = &cobra.Command{
Use: "md2confl",
Short: "md2confl - Convert markdown text to confluence wiki",
Expand All @@ -15,13 +19,22 @@ var RootCmd = &cobra.Command{
SilenceUsage: true,
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of md2confl",
Long: "Print the version number of md2confl",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("v%v\n", VERSION)
},
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
fmt.Fprintf(os.Stdout, "Error: %v\n", err)
}
}

func init() {
cobra.EnableCommandSorting = false
RootCmd.AddCommand(versionCmd)
}
13 changes: 11 additions & 2 deletions confluence/confluence.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package confluence

import (
"fmt"

"github.com/kentaro-m/blackfriday"
)

func Convert(input []byte) string {
type Confluence struct {
Contents string
}

func (c *Confluence) Convert(input []byte) {
renderer := blackfriday.ConfluenceRenderer(0)
extensions := 0
extensions |= blackfriday.EXTENSION_FENCED_CODE
Expand All @@ -13,6 +19,9 @@ func Convert(input []byte) string {
extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

output := blackfriday.Markdown(input, renderer, extensions)
c.Contents = string(output)
}

return string(output)
func (c *Confluence) Preview() {
fmt.Println(c.Contents)
}
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 0 additions & 24 deletions util/util.go

This file was deleted.

29 changes: 29 additions & 0 deletions utils/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package file

import (
"path/filepath"
"io/ioutil"
"errors"
)

type File struct {
Data []byte
}

func (f *File) Open(path string) error {
ext := filepath.Ext(path)

if ext != ".md" && ext != ".markdown" {
return errors.New("This file is not in markdown format.")
}

data, err := ioutil.ReadFile(path)

if err != nil {
return errors.New("Can't open this file.")
}

f.Data = data

return nil
}
20 changes: 20 additions & 0 deletions utils/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import (
"fmt"
"log"

"github.com/atotto/clipboard"
)

func Copy(text string) error {
err := clipboard.WriteAll(text)

if err != nil {
log.Fatal(err)
return err
}

fmt.Println("Copied to clipboard!")
return nil
}

0 comments on commit e9be560

Please sign in to comment.