initial commit

This commit is contained in:
Steven Polley 2024-08-15 10:45:07 -06:00
parent 577dde6894
commit 772e803782
6 changed files with 175 additions and 1 deletions

62
.drone.yml Normal file
View File

@ -0,0 +1,62 @@
kind: pipeline
name: default
workspace:
base: /go
path: src/deadbeef.codes/steven/txt2doc
steps:
- name: build txt2doc linux-amd64
image: golang
pull: always
volumes:
- name: publicrelease
path: /dist
environment:
GOOS: linux
GOARCH: amd64
CGO_ENABLED: 0
commands:
- go version
- go get
- go build -a -ldflags '-w' -o /dist/txt2doc-linux-amd64 .
- name: build txt2doc windows-amd64
image: golang
pull: always
volumes:
- name: publicrelease
path: /dist
environment:
GOOS: windows
GOARCH: amd64
CGO_ENABLED: 0
commands:
- go version
- go get
- go build -a -ldflags '-w' -o /dist/txt2doc-windows-amd64.exe .
- name: release
image: plugins/gitea-release
pull: always
volumes:
- name: publicrelease
path: /dist
settings:
api_key:
from_secret: drone_token
base_url: https://code.stevenpolley.net
files: /dist/*
when:
event:
- tag
volumes:
- name: publicrelease
host:
path: /data/public/build/txt2doc

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.exe

View File

@ -1,3 +1,17 @@
# txt2doc # txt2doc
Convert text files to doc files with no formatting [![Build Status](https://drone.stevenpolley.net/api/badges/steven/txt2doc/status.svg)](https://drone.stevenpolley.net/steven/txt2doc)
Convert text files to doc files with no formatting. See the releases page for pre-compiled binaries availabel for Windows and Linux on x86-64.
### Usage
```bash
./txt2doc.exe -f=<Text Input File> -o=<DOC Output File>
```
### Build from source
1. Ensure you have golang toolchain installed from https://go.dev
2. Clone this repository
3. Run the command go build <path to repo>

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module code.stevenpolley.net/steven/txt2doc
go 1.22.6
require github.com/gingfrederik/docx v0.0.1

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/gingfrederik/docx v0.0.1 h1:XciAehRNcFThJnH1ESfOb7amAYk6IGkvFHtVyTNn0oM=
github.com/gingfrederik/docx v0.0.1/go.mod h1:0+v8qYUEEQr66ZKvnQKVhrZBX59pG1MSsQpTYSYOC0A=

90
main.go Normal file
View File

@ -0,0 +1,90 @@
package main
import (
"flag"
"fmt"
"io"
"os"
"github.com/gingfrederik/docx"
)
var docPath = flag.String("o", "", "Path to the DOC output file")
var txtPath = flag.String("f", "", "Path to the text input file")
func usage() {
fmt.Printf(`%s: -f=<Text Input File> -o=<DOC Output File>
`,
os.Args[0])
}
/*
func generateXLSXFromCSV(csvPath string, XLSXPath string, delimiter string) error {
csvFile, err := os.Open(csvPath)
if err != nil {
return err
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
if len(delimiter) > 0 {
reader.Comma = rune(delimiter[0])
} else {
reader.Comma = rune(';')
}
xlsxFile := xlsx.NewFile()
sheet, err := xlsxFile.AddSheet(csvPath)
if err != nil {
return err
}
fields, err := reader.Read()
for err == nil {
row := sheet.AddRow()
for _, field := range fields {
cell := row.AddCell()
cell.Value = field
}
fields, err = reader.Read()
}
if err != nil {
fmt.Printf(err.Error())
}
return xlsxFile.Save(XLSXPath)
}*/
func main() {
flag.Parse()
if len(os.Args) < 2 {
usage()
return
}
flag.Parse()
err := generateDOCFromText(*txtPath, *docPath)
if err != nil {
fmt.Printf(err.Error())
return
}
}
func generateDOCFromText(txtPath string, docPath string) error {
txtFile, err := os.Open(txtPath)
if err != nil {
return fmt.Errorf("failed to open text file: %v", err)
}
defer txtFile.Close()
b, err := io.ReadAll(txtFile)
if err != nil {
return fmt.Errorf("failed to read text file: %v", err)
}
docFile := docx.NewFile()
para := docFile.AddParagraph()
para.AddText(string(b)).Size(9)
err = docFile.Save(docPath)
if err != nil {
return fmt.Errorf("failed to save doc file: %v", err)
}
return nil
}