Browse Source

对外暴露字幕解析的结构体

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
3879f25fc1

+ 6 - 2
internal/logic/sub_parser/ass/ass.go

@@ -32,7 +32,9 @@ func (p Parser) GetParserName() string {
 func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
 	nowExt := filepath.Ext(filePath)
 
-	p.log.Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
+	if p.log != nil {
+		p.log.Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
+	}
 
 	fBytes, err := os.ReadFile(filePath)
 	if err != nil {
@@ -53,7 +55,9 @@ func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool,
 	// 找到 start end text
 	matched := regex_things.ReMatchDialogueASS.FindAllStringSubmatch(allString, -1)
 	if matched == nil || len(matched) < 1 {
-		p.log.Debugln("DetermineFileTypeFromBytes can't found DialoguesFilter, Skip")
+		if p.log != nil {
+			p.log.Debugln("DetermineFileTypeFromBytes can't found DialoguesFilter, Skip")
+		}
 		return false, nil, nil
 	}
 	subFileInfo := subparser.FileInfo{}

+ 7 - 2
internal/logic/sub_parser/srt/srt.go

@@ -30,9 +30,12 @@ func (p Parser) GetParserName() string {
 	需要额外的处理逻辑,比如不用报错,而是跳过后续的逻辑
 */
 func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
+
 	nowExt := filepath.Ext(filePath)
 
-	p.log.Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
+	if p.log != nil {
+		p.log.Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
+	}
 
 	fBytes, err := os.ReadFile(filePath)
 	if err != nil {
@@ -56,7 +59,9 @@ func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool,
 
 	orgDialogues := p.parseContent(inBytes)
 	if len(orgDialogues) <= 0 {
-		p.log.Debugln("DetermineFileTypeFromBytes can't found DialoguesFilter, Skip")
+		if p.log != nil {
+			p.log.Debugln("DetermineFileTypeFromBytes can't found DialoguesFilter, Skip")
+		}
 		return false, nil, nil
 	}
 	subFileInfo.Dialogues = orgDialogues

+ 3 - 1
internal/pkg/sub_parser_hub/subParserHub.go

@@ -79,7 +79,9 @@ func (p SubParserHub) IsSubHasChinese(fileInfo *subparser.FileInfo) bool {
 
 	// 增加判断已存在的字幕是否有中文
 	if language.HasChineseLang(fileInfo.Lang) == false {
-		p.log.Warnln("IsSubHasChinese.HasChineseLang", fileInfo.FileFullPath, "not chinese sub, is ", fileInfo.Lang.String())
+		if p.log != nil {
+			p.log.Warnln("IsSubHasChinese.HasChineseLang", fileInfo.FileFullPath, "not chinese sub, is ", fileInfo.Lang.String())
+		}
 		return false
 	}
 

+ 16 - 0
pkg/sub_parser_hub/sub_parser_hub.go

@@ -0,0 +1,16 @@
+package sub_parser_hub
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
+	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
+)
+
+type SubParserHub struct {
+	*sub_parser_hub.SubParserHub
+}
+
+func NewSubParserHub() *SubParserHub {
+
+	return &SubParserHub{sub_parser_hub.NewSubParserHub(nil, ass.NewParser(nil), srt.NewParser(nil))}
+}