Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kemokemo committed Jun 28, 2020
0 parents commit 99068fa
Show file tree
Hide file tree
Showing 21 changed files with 456 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
version: 2.1

orbs:
codecov: codecov/[email protected]

executors:
go-114:
docker:
# https://hub.docker.com/layers/circleci/golang/1.14-node/images/sha256-2190fa9a5f81e3cb4397147d11aeaa88c22e6e35d401eead90195925230e071d?context=explore
- image: circleci/golang:1.14-node
working_directory: /go/src/github.com/kemokemo/furit

jobs:
test:
executor: go-114
steps:
- checkout
- run: go version
- run: go get -v -t -d ./...
- run: go test -v -cover -coverprofile=coverage.out ./...
- codecov/upload:
file: ./coverage.out
lint:
executor: go-114
steps:
- checkout
- run: go get -v -t -d ./...
# see here: https://github.com/golang/lint/issues/415#issuecomment-511234597
- run: go get -u golang.org/x/lint/golint
- run: golint ./...
- run: go vet ./...
build:
executor: go-114
steps:
- checkout
- run: go get -v -t -d ./...
- run: go build
deploy:
executor: go-114
steps:
- run: echo 'export PATH=${GOPATH}/bin/:${PATH}' >> $BASH_ENV
- checkout
- run: go get -v -t -d ./...
- run: go get github.com/mitchellh/gox
- run: go get github.com/tcnksm/ghr
- run: mkdir release
- run: gox -output "./release/{{.Dir}}_{{.OS}}_{{.Arch}}" ./ ./...
- run: ghr -u $CIRCLE_PROJECT_USERNAME $CIRCLE_TAG release/

workflows:
version: 2
test, lint and build:
jobs:
- test
- lint
- build:
requires:
- lint
deploy:
jobs:
- build:
filters:
tags:
only: /.*/
- deploy:
requires:
- build
filters:
tags:
only: /^v.*/
branches:
ignore: /.*/
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
furit

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# OS files
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 kemokemo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# furit ( <u>F</u>ind <u>U</u>n<u>r</u>eferenced <u>I</u>mages in <u>T</u>ext files )

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) [![CircleCI](https://circleci.com/gh/kemokemo/furit.svg?style=svg)](https://circleci.com/gh/kemokemo/furit) [![codecov](https://codecov.io/gh/kemokemo/furit/branch/master/graph/badge.svg)](https://codecov.io/gh/kemokemo/furit) [![Go Report Card](https://goreportcard.com/badge/github.com/kemokemo/furit)](https://goreportcard.com/report/github.com/kemokemo/furit)

This tool finds unreferenced image assets from text files such as markdown.

## Install

### Binary

Get the latest version from [the release page](https://github.com/kemokemo/furit/releases/latest), and download the archive file for your operating system/architecture. Unpack the archive, and put the binary somewhere in your `$PATH`.

## Usage

```sh
$ furit -h
Usage: furit [<option>...] <1st path> <2nd path>...
you can set mutiple paths to search invalid images.

-h display help
```

Currently, only Markdown format is supported as text to find links to images.

### Example

```
$ furit lib/test-data/markdown
lib/test-data/markdown/assets/画面.bmp
lib/test-data/markdown/テスト.gif
```

This tool looks recursively for the folder you specify, finds links to images in the text it finds, and enumerates the unlinked image files from text.

## License

[MIT](https://github.com/kemokemo/furit/blob/master/LICENSE)

## Author

[kemokemo](https://github.com/kemokemo)

37 changes: 37 additions & 0 deletions lib/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package furit

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

type imageFinder struct{}

const imgFileFormat = `\.(png|PNG|jpg|JPG|jpeg|JPEG|bmp|BMP|gif|GIF|tiff|TIFF)`

var imgFileReg = regexp.MustCompile(imgFileFormat)

// Find finds image files and returns paths to the program.
func (i *imageFinder) Find(root string) ([]string, error) {
var imgPaths []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil {
return fmt.Errorf("failed to walk for image: %v", err)
}
// Exclude directories with names beginning with a dot. ex) .git, .node_modules etc..
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}

if info.IsDir() || !imgFileReg.MatchString(info.Name()) {
return nil
}
imgPaths = append(imgPaths, path)
return nil
})

return imgPaths, err
}
41 changes: 41 additions & 0 deletions lib/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package furit

import (
"reflect"
"testing"
)

func Test_imageFinder_Find(t *testing.T) {
type args struct {
root string
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{name: "test-data", args: args{root: "./test-data/image-files"},
want: []string{
"test-data/image-files/blank.jpg",
"test-data/image-files/sample.png",
"test-data/image-files/画像/テスト.gif",
"test-data/image-files/画面.bmp",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &imageFinder{}
got, err := i.Find(tt.args.root)
if (err != nil) != tt.wantErr {
t.Errorf("imageFinder.Find() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("imageFinder.Find() = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 10 additions & 0 deletions lib/img-finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package furit

// ImageFinder finds image files
type ImageFinder interface {
// Find finds image files and returns paths to the program.
Find(root string) ([]string, error)
}

// Image is the finder of image files.
var Image ImageFinder = (*imageFinder)(nil)
10 changes: 10 additions & 0 deletions lib/link-finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package furit

// ImageLinkFinder will search files and find image links.
type ImageLinkFinder interface {
// Find converts the path of images referenced by text to paths relative to the program and returns it.
Find(root string) ([]string, error)
}

// Markdown is the finder of image link from markdown file.
var Markdown ImageLinkFinder = (*markdown)(nil)
52 changes: 52 additions & 0 deletions lib/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package furit

import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

type markdown struct{}

const mdImgLinkFormat = `\!\[.*\]\((.+\.(png|PNG|jpg|JPG|jpeg|JPEG|bmp|BMP|gif|GIF|tiff|TIFF))\)`

var mdLinkReg = regexp.MustCompile(mdImgLinkFormat)

// Find converts the path of images referenced by text to paths relative to the program and returns it.
func (m *markdown) Find(root string) ([]string, error) {
var links []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil {
return fmt.Errorf("failed to walk for markdown: %v", err)
}
// Exclude directories with names beginning with a dot. ex) .git, .node_modules etc..
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}

ext := filepath.Ext(info.Name())
if info.IsDir() || (ext != ".md" && ext != ".markdown") {
return nil
}

f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

s := bufio.NewScanner(f)
for s.Scan() {
group := mdLinkReg.FindSubmatch([]byte(s.Text()))
if len(group) > 1 {
links = append(links, filepath.Join(filepath.Dir(path), string(group[1])))
}
}
return nil
})

return links, err
}
40 changes: 40 additions & 0 deletions lib/markdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package furit

import (
"reflect"
"testing"
)

func Test_markdown_Find(t *testing.T) {
type args struct {
root string
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{name: "test-data", args: args{root: "test-data/markdown"},
want: []string{
"test-data/markdown/posts/assets/gopher.png",
"test-data/markdown/assets/sample1.png",
"test-data/markdown/assets/サンプル.png",
"test-data/markdown/logo.jpg",
"test-data/markdown/テスト.png",
}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &markdown{}
got, err := m.Find(tt.args.root)
if (err != nil) != tt.wantErr {
t.Errorf("markdown.Find() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("markdown.Find() = %v, want %v", got, tt.want)
}
})
}
}
Binary file added lib/test-data/image-files/blank.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/test-data/image-files/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/test-data/image-files/画像/テスト.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/test-data/image-files/画面.bmp
Binary file not shown.
Binary file added lib/test-data/markdown/assets/sample1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/test-data/markdown/assets/画面.bmp
Binary file not shown.
Binary file added lib/test-data/markdown/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions lib/test-data/markdown/posts/post1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# my first post

## Hey!

![gopher](assets/gopher.png)
13 changes: 13 additions & 0 deletions lib/test-data/markdown/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# test markdown

## Chapter 1

![sample](assets/sample1.png)

![サンプル](assets/サンプル.png)

## Chapter 2

![logo](logo.jpg)

![](テスト.png)
Binary file added lib/test-data/markdown/テスト.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 99068fa

Please sign in to comment.