Skip to content

Commit

Permalink
rework to only export a few functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed Dec 16, 2015
1 parent c230a8e commit 695f068
Show file tree
Hide file tree
Showing 32 changed files with 417 additions and 472 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
go test -v
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Subber is a cli tool and library for reading,
writing and manipulating .srt subtitle files written in Go

WARNING: The API is unstable, work in progress!

### Features

Expand All @@ -22,10 +23,10 @@ and the `subber` cli app automates this task.

# Installation

Assuming golang is installed on your system:
To install the command line:

```
go install github.com/martinlindhe/subber
go install github.com/martinlindhe/subber/subber
```


Expand Down
12 changes: 5 additions & 7 deletions filter/caps.go → caps.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package filter
package subber

import (
"fmt"
"log"
"strings"

"github.com/martinlindhe/subber/caption"
)

// CapsFixer converts "ALL CAPS" text into "Initial letter capped"
func CapsFixer(captions []caption.Caption) []caption.Caption {
// filterCapitalization converts "ALL CAPS" text into "Initial letter capped"
func filterCapitalization(captions []caption) []caption {

for _, cap := range captions {
for i, line := range cap.Text {

clean := ucFirst(line)

if clean != cap.Text[i] {
fmt.Printf("[caps] %s -> %s\n", cap.Text[i], clean)
log.Printf("[caps] %s -> %s\n", cap.Text[i], clean)
cap.Text[i] = clean
}
}
Expand Down
30 changes: 30 additions & 0 deletions caps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package subber

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilterCapitalization(t *testing.T) {

var in = []caption{
{
Seq: 1,
Start: makeTime(0, 0, 4, 630),
End: makeTime(0, 0, 6, 18),
Text: []string{"GO NINJA!", "NINJA GO!"},
},
}

var expected = []caption{
{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
[]string{"Go ninja!", "Ninja go!"},
},
}

assert.Equal(t, expected, filterCapitalization(in))
}
11 changes: 11 additions & 0 deletions caption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package subber

import "time"

// caption represents one subtitle block
type caption struct {
Seq int
Start time.Time
End time.Time
Text []string
}
23 changes: 0 additions & 23 deletions caption/caption.go

This file was deleted.

20 changes: 0 additions & 20 deletions caption/caption_test.go

This file was deleted.

19 changes: 19 additions & 0 deletions caption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package subber

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRenderTime(t *testing.T) {

cap := caption{
1,
makeTime(18, 40, 22, 110),
makeTime(18, 41, 20, 123),
[]string{"<i>Go ninja!</i>"},
}

assert.Equal(t, "18:40:22,110 --> 18:41:20,123", cap.srtTime())
}
27 changes: 20 additions & 7 deletions cleaner/cleaner.go → cleaner.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package cleaner
package subber

import (
"fmt"
"log"
"strings"

"github.com/martinlindhe/subber/caption"
)

// CleanupSub performs cleanup on .srt data, returning a string. caller is responsible for passing UTF8 string
func CleanupSub(utf8 string, filterName string, keepAds bool) (string, error) {

captions := parseSrt(utf8)
if !keepAds {
captions = removeAds(captions)
}

captions = filterSubs(captions, filterName)

out := renderSrt(captions)

return out, nil
}

// RemoveAds removes advertisement from the subtitles
func RemoveAds(subs []caption.Caption) []caption.Caption {
func removeAds(subs []caption) []caption {

var res []caption.Caption
var res []caption

ads := []string{

Expand Down Expand Up @@ -79,7 +92,7 @@ func RemoveAds(subs []caption.Caption) []caption.Caption {
for _, adLine := range ads {
if !isAd && strings.Contains(x, adLine) {
isAd = true
fmt.Println("[ads]", orgSeq, sub.Text, "matched", adLine)
log.Println("[ads]", orgSeq, sub.Text, "matched", adLine)
break
}
}
Expand Down
50 changes: 0 additions & 50 deletions cleaner/cleaner_test.go

This file was deleted.

48 changes: 48 additions & 0 deletions cleaner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package subber

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRemoveAds(t *testing.T) {

var in = []caption{
{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
[]string{"Go ninja!"},
},
{
2,
makeTime(0, 1, 9, 630),
makeTime(0, 1, 11, 005),
[]string{"Subtitles By MrCool"},
},
{
3,
makeTime(0, 1, 9, 630),
makeTime(0, 1, 11, 005),
[]string{"No ninja!"},
},
}

var expected = []caption{
{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
[]string{"Go ninja!"},
},
{
2,
makeTime(0, 1, 9, 630),
makeTime(0, 1, 11, 005),
[]string{"No ninja!"},
},
}

assert.Equal(t, expected, removeAds(in))
}
21 changes: 17 additions & 4 deletions txtformat/encoding.go → encoding.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package txtformat
package subber

import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"unicode/utf16"
"unicode/utf8"
)

// ReadFileAsUTF8 reads text file, tries to convert to UTF8
func ReadFileAsUTF8(fileName string) (string, error) {

data, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}

utf8 := convertToUTF8(data)
return utf8, nil
}

// ConvertToUTF8 returns an utf8 string
func ConvertToUTF8(b []byte) string {
func convertToUTF8(b []byte) string {

s := ""

Expand All @@ -25,11 +38,11 @@ func ConvertToUTF8(b []byte) string {
s = string(b)
}

return NormalizeLineFeeds(s)
return normalizeLineFeeds(s)
}

// NormalizeLineFeeds will return a string with \n as linefeeds
func NormalizeLineFeeds(s string) string {
func normalizeLineFeeds(s string) string {

if len(s) < 80 {
return s
Expand Down
2 changes: 1 addition & 1 deletion txtformat/encoding_test.go → encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package txtformat
package subber

import (
"testing"
Expand Down
21 changes: 21 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package subber

import (
"fmt"
)

// filterSubs pass the captions through a filter function
func filterSubs(captions []caption, filter string) []caption {

if filter == "caps" {
return filterCapitalization(captions)
}
if filter == "html" {
return filterHTML(captions)
}
if filter != "none" {
fmt.Printf("Unrecognized filter name: %s\n", filter)
}

return captions
}
Loading

0 comments on commit 695f068

Please sign in to comment.