Skip to content

Commit 6317ce2

Browse files
committed
refactor(exporters): replace deprecated strings.Title with cases.Title
The `strings.Title` function is deprecated because it does not handle Unicode punctuation correctly. This change replaces its usage in the DOCX, HTML, and Markdown exporters with the recommended `golang.org/x/text/cases` package. This ensures more robust and accurate title-casing for item headings.
1 parent 59f2de9 commit 6317ce2

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/kjanat/articulate-parser
22

33
go 1.24.0
44

5-
require github.com/fumiama/go-docx v0.0.0-20250506085032-0c30fd09304b
5+
require (
6+
github.com/fumiama/go-docx v0.0.0-20250506085032-0c30fd09304b
7+
golang.org/x/text v0.30.0
8+
)
69

710
require (
811
github.com/fumiama/imgsz v0.0.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ github.com/fumiama/imgsz v0.0.4 h1:Lsasu2hdSSFS+vnD+nvR1UkiRMK7hcpyYCC0FzgSMFI=
44
github.com/fumiama/imgsz v0.0.4/go.mod h1:bISOQVTlw9sRytPwe8ir7tAaEmyz9hSNj9n8mXMBG0E=
55
golang.org/x/image v0.32.0 h1:6lZQWq75h7L5IWNk0r+SCpUJ6tUVd3v4ZHnbRKLkUDQ=
66
golang.org/x/image v0.32.0/go.mod h1:/R37rrQmKXtO6tYXAjtDLwQgFLHmhW+V6ayXlxzP2Pc=
7+
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
8+
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=

internal/exporters/docx.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/kjanat/articulate-parser/internal/interfaces"
1212
"github.com/kjanat/articulate-parser/internal/models"
1313
"github.com/kjanat/articulate-parser/internal/services"
14+
"golang.org/x/text/cases"
15+
"golang.org/x/text/language"
1416
)
1517

1618
// DocxExporter implements the Exporter interface for DOCX format.
@@ -119,7 +121,8 @@ func (e *DocxExporter) exportItem(doc *docx.Docx, item *models.Item) {
119121
// Add item type as heading
120122
if item.Type != "" {
121123
itemPara := doc.AddParagraph()
122-
itemPara.AddText(strings.Title(item.Type)).Size("24").Bold()
124+
caser := cases.Title(language.English)
125+
itemPara.AddText(caser.String(item.Type)).Size("24").Bold()
123126
}
124127

125128
// Add sub-items

internal/exporters/html.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/kjanat/articulate-parser/internal/interfaces"
1313
"github.com/kjanat/articulate-parser/internal/models"
1414
"github.com/kjanat/articulate-parser/internal/services"
15+
"golang.org/x/text/cases"
16+
"golang.org/x/text/language"
1517
)
1618

1719
// HTMLExporter implements the Exporter interface for HTML format.
@@ -441,7 +443,8 @@ func (e *HTMLExporter) processDividerItem(buf *bytes.Buffer) {
441443
func (e *HTMLExporter) processUnknownItem(buf *bytes.Buffer, item models.Item) {
442444
if len(item.Items) > 0 {
443445
buf.WriteString(" <div class=\"item unknown-item\">\n")
444-
buf.WriteString(fmt.Sprintf(" <h4>%s Content</h4>\n", strings.Title(item.Type)))
446+
caser := cases.Title(language.English)
447+
buf.WriteString(fmt.Sprintf(" <h4>%s Content</h4>\n", caser.String(item.Type)))
445448
for _, subItem := range item.Items {
446449
e.processGenericSubItem(buf, subItem)
447450
}

internal/exporters/markdown.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/kjanat/articulate-parser/internal/interfaces"
1212
"github.com/kjanat/articulate-parser/internal/models"
1313
"github.com/kjanat/articulate-parser/internal/services"
14+
"golang.org/x/text/cases"
15+
"golang.org/x/text/language"
1416
)
1517

1618
// MarkdownExporter implements the Exporter interface for Markdown format.
@@ -269,7 +271,8 @@ func (e *MarkdownExporter) processDividerItem(buf *bytes.Buffer) {
269271
// processUnknownItem handles unknown or unsupported item types
270272
func (e *MarkdownExporter) processUnknownItem(buf *bytes.Buffer, item models.Item, headingPrefix string) {
271273
if len(item.Items) > 0 {
272-
buf.WriteString(fmt.Sprintf("%s %s Content\n\n", headingPrefix, strings.Title(item.Type)))
274+
caser := cases.Title(language.English)
275+
buf.WriteString(fmt.Sprintf("%s %s Content\n\n", headingPrefix, caser.String(item.Type)))
273276
for _, subItem := range item.Items {
274277
e.processGenericSubItem(buf, subItem)
275278
}

0 commit comments

Comments
 (0)