refactor: Align with Go conventions and improve maintainability

Renames the `OriginalUrl` field to `OriginalURL` across media models to adhere to Go's common initialisms convention. The `json` tag is unchanged to maintain API compatibility.

Introduces constants for exporter formats (e.g., `FormatMarkdown`, `FormatDocx`) to eliminate the use of magic strings, enhancing type safety and making the code easier to maintain.

Additionally, this commit includes several minor code quality improvements:
- Wraps file-writing errors in exporters to provide more context.
- Removes redundant package-level comments from test files.
- Applies various minor linting fixes throughout the codebase.
This commit is contained in:
2025-11-06 16:45:25 +01:00
parent d8e4d97841
commit b56c9fa29f
26 changed files with 53 additions and 77 deletions

View File

@ -467,7 +467,7 @@ jobs:
# Process a local file (mount current directory) # Process a local file (mount current directory)
docker run --rm -v $(pwd):/workspace ghcr.io/${{ github.repository }}:latest /workspace/input.json markdown /workspace/output.md docker run --rm -v $(pwd):/workspace ghcr.io/${{ github.repository }}:latest /workspace/input.json markdown /workspace/output.md
``` ```
EOF EOF
# Security and quality analysis workflows # Security and quality analysis workflows

View File

@ -1,4 +1,3 @@
// Package config_test provides tests for the config package.
package config package config
import ( import (

View File

@ -1,4 +1,3 @@
// Package exporters_test provides benchmarks for all exporters.
package exporters package exporters
import ( import (

View File

@ -69,7 +69,7 @@ func (e *DocxExporter) Export(course *models.Course, outputPath string) error {
// Ensure output directory exists and add .docx extension // Ensure output directory exists and add .docx extension
if !strings.HasSuffix(strings.ToLower(outputPath), ".docx") { if !strings.HasSuffix(strings.ToLower(outputPath), ".docx") {
outputPath = outputPath + ".docx" outputPath += ".docx"
} }
// Create the file // Create the file
@ -198,5 +198,5 @@ func (e *DocxExporter) exportSubItem(doc *docx.Docx, subItem *models.SubItem) {
// Returns: // Returns:
// - A string representing the supported format ("docx") // - A string representing the supported format ("docx")
func (e *DocxExporter) SupportedFormat() string { func (e *DocxExporter) SupportedFormat() string {
return "docx" return FormatDocx
} }

View File

@ -1,4 +1,3 @@
// Package exporters_test provides tests for the docx exporter.
package exporters package exporters
import ( import (
@ -331,7 +330,7 @@ func TestDocxExporter_ComplexCourse(t *testing.T) {
Caption: "<p>Watch this introductory video</p>", Caption: "<p>Watch this introductory video</p>",
Media: &models.Media{ Media: &models.Media{
Video: &models.VideoMedia{ Video: &models.VideoMedia{
OriginalUrl: "https://example.com/intro.mp4", OriginalURL: "https://example.com/intro.mp4",
Duration: 300, Duration: 300,
}, },
}, },
@ -359,7 +358,7 @@ func TestDocxExporter_ComplexCourse(t *testing.T) {
Caption: "<p>Course overview diagram</p>", Caption: "<p>Course overview diagram</p>",
Media: &models.Media{ Media: &models.Media{
Image: &models.ImageMedia{ Image: &models.ImageMedia{
OriginalUrl: "https://example.com/overview.png", OriginalURL: "https://example.com/overview.png",
}, },
}, },
}, },

View File

@ -1,5 +1,3 @@
// Package exporters provides implementations of the Exporter interface
// for converting Articulate Rise courses into various file formats.
package exporters package exporters
import ( import (
@ -10,6 +8,13 @@ import (
"github.com/kjanat/articulate-parser/internal/services" "github.com/kjanat/articulate-parser/internal/services"
) )
// Format constants for supported export formats.
const (
FormatMarkdown = "markdown"
FormatDocx = "docx"
FormatHTML = "html"
)
// Factory implements the ExporterFactory interface. // Factory implements the ExporterFactory interface.
// It creates appropriate exporter instances based on the requested format. // It creates appropriate exporter instances based on the requested format.
type Factory struct { type Factory struct {
@ -36,11 +41,11 @@ func NewFactory(htmlCleaner *services.HTMLCleaner) interfaces.ExporterFactory {
// Format strings are case-insensitive (e.g., "markdown", "DOCX"). // Format strings are case-insensitive (e.g., "markdown", "DOCX").
func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) { func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) {
switch strings.ToLower(format) { switch strings.ToLower(format) {
case "markdown", "md": case FormatMarkdown, "md":
return NewMarkdownExporter(f.htmlCleaner), nil return NewMarkdownExporter(f.htmlCleaner), nil
case "docx", "word": case FormatDocx, "word":
return NewDocxExporter(f.htmlCleaner), nil return NewDocxExporter(f.htmlCleaner), nil
case "html", "htm": case FormatHTML, "htm":
return NewHTMLExporter(f.htmlCleaner), nil return NewHTMLExporter(f.htmlCleaner), nil
default: default:
return nil, fmt.Errorf("unsupported export format: %s", format) return nil, fmt.Errorf("unsupported export format: %s", format)
@ -50,5 +55,5 @@ func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) {
// SupportedFormats returns a list of all supported export formats, // SupportedFormats returns a list of all supported export formats,
// including both primary format names and their aliases. // including both primary format names and their aliases.
func (f *Factory) SupportedFormats() []string { func (f *Factory) SupportedFormats() []string {
return []string{"markdown", "md", "docx", "word", "html", "htm"} return []string{FormatMarkdown, "md", FormatDocx, "word", FormatHTML, "htm"}
} }

View File

@ -1,4 +1,3 @@
// Package exporters_test provides tests for the exporter factory.
package exporters package exporters
import ( import (

View File

@ -1,5 +1,3 @@
// Package exporters provides implementations of the Exporter interface
// for converting Articulate Rise courses into various file formats.
package exporters package exporters
import ( import (
@ -112,7 +110,10 @@ func (e *HTMLExporter) Export(course *models.Course, outputPath string) error {
buf.WriteString("</html>\n") buf.WriteString("</html>\n")
// #nosec G306 - 0644 is appropriate for export files that should be readable by others // #nosec G306 - 0644 is appropriate for export files that should be readable by others
return os.WriteFile(outputPath, buf.Bytes(), 0o644) if err := os.WriteFile(outputPath, buf.Bytes(), 0o644); err != nil {
return fmt.Errorf("failed to write HTML file: %w", err)
}
return nil
} }
// SupportedFormat returns the format name this exporter supports // SupportedFormat returns the format name this exporter supports
@ -121,7 +122,7 @@ func (e *HTMLExporter) Export(course *models.Course, outputPath string) error {
// Returns: // Returns:
// - A string representing the supported format ("html") // - A string representing the supported format ("html")
func (e *HTMLExporter) SupportedFormat() string { func (e *HTMLExporter) SupportedFormat() string {
return "html" return FormatHTML
} }
// getDefaultCSS returns basic CSS styling for the HTML document. // getDefaultCSS returns basic CSS styling for the HTML document.
@ -390,7 +391,7 @@ func (e *HTMLExporter) processMultimediaItem(buf *bytes.Buffer, item models.Item
if subItem.Media != nil { if subItem.Media != nil {
if subItem.Media.Video != nil { if subItem.Media.Video != nil {
buf.WriteString(" <div class=\"media-info\">\n") buf.WriteString(" <div class=\"media-info\">\n")
fmt.Fprintf(buf, " <p><strong>Video:</strong> %s</p>\n", html.EscapeString(subItem.Media.Video.OriginalUrl)) fmt.Fprintf(buf, " <p><strong>Video:</strong> %s</p>\n", html.EscapeString(subItem.Media.Video.OriginalURL))
if subItem.Media.Video.Duration > 0 { if subItem.Media.Video.Duration > 0 {
fmt.Fprintf(buf, " <p><strong>Duration:</strong> %d seconds</p>\n", subItem.Media.Video.Duration) fmt.Fprintf(buf, " <p><strong>Duration:</strong> %d seconds</p>\n", subItem.Media.Video.Duration)
} }
@ -411,7 +412,7 @@ func (e *HTMLExporter) processImageItem(buf *bytes.Buffer, item models.Item) {
for _, subItem := range item.Items { for _, subItem := range item.Items {
if subItem.Media != nil && subItem.Media.Image != nil { if subItem.Media != nil && subItem.Media.Image != nil {
buf.WriteString(" <div class=\"media-info\">\n") buf.WriteString(" <div class=\"media-info\">\n")
fmt.Fprintf(buf, " <p><strong>Image:</strong> %s</p>\n", html.EscapeString(subItem.Media.Image.OriginalUrl)) fmt.Fprintf(buf, " <p><strong>Image:</strong> %s</p>\n", html.EscapeString(subItem.Media.Image.OriginalURL))
buf.WriteString(" </div>\n") buf.WriteString(" </div>\n")
} }
if subItem.Caption != "" { if subItem.Caption != "" {

View File

@ -1,4 +1,3 @@
// Package exporters_test provides tests for the html exporter.
package exporters package exporters
import ( import (
@ -270,7 +269,7 @@ func TestHTMLExporter_ProcessMultimediaItem(t *testing.T) {
Title: "<p>Video Title</p>", Title: "<p>Video Title</p>",
Media: &models.Media{ Media: &models.Media{
Video: &models.VideoMedia{ Video: &models.VideoMedia{
OriginalUrl: "https://example.com/video.mp4", OriginalURL: "https://example.com/video.mp4",
Duration: 120, Duration: 120,
}, },
}, },
@ -315,7 +314,7 @@ func TestHTMLExporter_ProcessImageItem(t *testing.T) {
{ {
Media: &models.Media{ Media: &models.Media{
Image: &models.ImageMedia{ Image: &models.ImageMedia{
OriginalUrl: "https://example.com/image.png", OriginalURL: "https://example.com/image.png",
}, },
}, },
Caption: "<p>Image caption</p>", Caption: "<p>Image caption</p>",

View File

@ -1,5 +1,3 @@
// Package exporters provides implementations of the Exporter interface
// for converting Articulate Rise courses into various file formats.
package exporters package exporters
import ( import (
@ -82,12 +80,15 @@ func (e *MarkdownExporter) Export(course *models.Course, outputPath string) erro
} }
// #nosec G306 - 0644 is appropriate for export files that should be readable by others // #nosec G306 - 0644 is appropriate for export files that should be readable by others
return os.WriteFile(outputPath, buf.Bytes(), 0o644) if err := os.WriteFile(outputPath, buf.Bytes(), 0o644); err != nil {
return fmt.Errorf("failed to write markdown file: %w", err)
}
return nil
} }
// SupportedFormat returns "markdown". // SupportedFormat returns "markdown".
func (e *MarkdownExporter) SupportedFormat() string { func (e *MarkdownExporter) SupportedFormat() string {
return "markdown" return FormatMarkdown
} }
// processItemToMarkdown converts a course item into Markdown format. // processItemToMarkdown converts a course item into Markdown format.
@ -170,7 +171,7 @@ func (e *MarkdownExporter) processMediaSubItem(buf *bytes.Buffer, subItem models
// processVideoMedia processes video media content. // processVideoMedia processes video media content.
func (e *MarkdownExporter) processVideoMedia(buf *bytes.Buffer, media *models.Media) { func (e *MarkdownExporter) processVideoMedia(buf *bytes.Buffer, media *models.Media) {
if media.Video != nil { if media.Video != nil {
fmt.Fprintf(buf, "**Video**: %s\n", media.Video.OriginalUrl) fmt.Fprintf(buf, "**Video**: %s\n", media.Video.OriginalURL)
if media.Video.Duration > 0 { if media.Video.Duration > 0 {
fmt.Fprintf(buf, "**Duration**: %d seconds\n", media.Video.Duration) fmt.Fprintf(buf, "**Duration**: %d seconds\n", media.Video.Duration)
} }
@ -180,7 +181,7 @@ func (e *MarkdownExporter) processVideoMedia(buf *bytes.Buffer, media *models.Me
// processImageMedia processes image media content. // processImageMedia processes image media content.
func (e *MarkdownExporter) processImageMedia(buf *bytes.Buffer, media *models.Media) { func (e *MarkdownExporter) processImageMedia(buf *bytes.Buffer, media *models.Media) {
if media.Image != nil { if media.Image != nil {
fmt.Fprintf(buf, "**Image**: %s\n", media.Image.OriginalUrl) fmt.Fprintf(buf, "**Image**: %s\n", media.Image.OriginalURL)
} }
} }
@ -189,7 +190,7 @@ func (e *MarkdownExporter) processImageItem(buf *bytes.Buffer, item models.Item,
fmt.Fprintf(buf, "%s Image\n\n", headingPrefix) fmt.Fprintf(buf, "%s Image\n\n", headingPrefix)
for _, subItem := range item.Items { for _, subItem := range item.Items {
if subItem.Media != nil && subItem.Media.Image != nil { if subItem.Media != nil && subItem.Media.Image != nil {
fmt.Fprintf(buf, "**Image**: %s\n", subItem.Media.Image.OriginalUrl) fmt.Fprintf(buf, "**Image**: %s\n", subItem.Media.Image.OriginalURL)
} }
if subItem.Caption != "" { if subItem.Caption != "" {
caption := e.htmlCleaner.CleanHTML(subItem.Caption) caption := e.htmlCleaner.CleanHTML(subItem.Caption)

View File

@ -1,4 +1,3 @@
// Package exporters_test provides tests for the markdown exporter.
package exporters package exporters
import ( import (
@ -188,7 +187,7 @@ func TestMarkdownExporter_ProcessMultimediaItem(t *testing.T) {
{ {
Media: &models.Media{ Media: &models.Media{
Video: &models.VideoMedia{ Video: &models.VideoMedia{
OriginalUrl: "https://example.com/video.mp4", OriginalURL: "https://example.com/video.mp4",
Duration: 120, Duration: 120,
}, },
}, },
@ -227,7 +226,7 @@ func TestMarkdownExporter_ProcessImageItem(t *testing.T) {
{ {
Media: &models.Media{ Media: &models.Media{
Image: &models.ImageMedia{ Image: &models.ImageMedia{
OriginalUrl: "https://example.com/image.jpg", OriginalURL: "https://example.com/image.jpg",
}, },
}, },
Caption: "<p>Image caption</p>", Caption: "<p>Image caption</p>",
@ -372,7 +371,7 @@ func TestMarkdownExporter_ProcessVideoMedia(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
media := &models.Media{ media := &models.Media{
Video: &models.VideoMedia{ Video: &models.VideoMedia{
OriginalUrl: "https://example.com/video.mp4", OriginalURL: "https://example.com/video.mp4",
Duration: 300, Duration: 300,
}, },
} }
@ -397,7 +396,7 @@ func TestMarkdownExporter_ProcessImageMedia(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
media := &models.Media{ media := &models.Media{
Image: &models.ImageMedia{ Image: &models.ImageMedia{
OriginalUrl: "https://example.com/image.jpg", OriginalURL: "https://example.com/image.jpg",
}, },
} }

View File

@ -1,5 +1,3 @@
// Package interfaces provides the core contracts for the articulate-parser application.
// It defines interfaces for parsing and exporting Articulate Rise courses.
package interfaces package interfaces
import "github.com/kjanat/articulate-parser/internal/models" import "github.com/kjanat/articulate-parser/internal/models"

View File

@ -1,5 +1,3 @@
// Package interfaces provides the core contracts for the articulate-parser application.
// It defines interfaces for parsing and exporting Articulate Rise courses.
package interfaces package interfaces
import "context" import "context"

View File

@ -1,5 +1,3 @@
// Package models defines the data structures representing Articulate Rise courses.
// These structures closely match the JSON format used by Articulate Rise.
package models package models
// Lesson represents a single lesson or section within an Articulate Rise course. // Lesson represents a single lesson or section within an Articulate Rise course.

View File

@ -1,5 +1,3 @@
// Package models defines the data structures representing Articulate Rise courses.
// These structures closely match the JSON format used by Articulate Rise.
package models package models
// Media represents a media element that can be either an image or a video. // Media represents a media element that can be either an image or a video.
@ -23,8 +21,8 @@ type ImageMedia struct {
Height int `json:"height,omitempty"` Height int `json:"height,omitempty"`
// CrushedKey is the identifier for a compressed version of the image // CrushedKey is the identifier for a compressed version of the image
CrushedKey string `json:"crushedKey,omitempty"` CrushedKey string `json:"crushedKey,omitempty"`
// OriginalUrl is the URL to the full-resolution image // OriginalURL is the URL to the full-resolution image
OriginalUrl string `json:"originalUrl"` OriginalURL string `json:"originalUrl"`
// UseCrushedKey indicates whether to use the compressed version // UseCrushedKey indicates whether to use the compressed version
UseCrushedKey bool `json:"useCrushedKey,omitempty"` UseCrushedKey bool `json:"useCrushedKey,omitempty"`
} }
@ -45,6 +43,6 @@ type VideoMedia struct {
InputKey string `json:"inputKey,omitempty"` InputKey string `json:"inputKey,omitempty"`
// Thumbnail is the URL to a smaller preview image // Thumbnail is the URL to a smaller preview image
Thumbnail string `json:"thumbnail,omitempty"` Thumbnail string `json:"thumbnail,omitempty"`
// OriginalUrl is the URL to the source video file // OriginalURL is the URL to the source video file
OriginalUrl string `json:"originalUrl"` OriginalURL string `json:"originalUrl"`
} }

View File

@ -1,4 +1,3 @@
// Package models_test provides tests for the data models.
package models package models
import ( import (
@ -98,7 +97,7 @@ func TestCourseInfo_JSONMarshalUnmarshal(t *testing.T) {
Type: "jpg", Type: "jpg",
Width: 800, Width: 800,
Height: 600, Height: 600,
OriginalUrl: "https://example.com/image.jpg", OriginalURL: "https://example.com/image.jpg",
}, },
}, },
} }
@ -149,7 +148,7 @@ func TestLesson_JSONMarshalUnmarshal(t *testing.T) {
URL: "https://example.com/video.mp4", URL: "https://example.com/video.mp4",
Type: "mp4", Type: "mp4",
Duration: 120, Duration: 120,
OriginalUrl: "https://example.com/video.mp4", OriginalURL: "https://example.com/video.mp4",
}, },
}, },
}, },
@ -244,7 +243,7 @@ func TestSubItem_JSONMarshalUnmarshal(t *testing.T) {
Type: "png", Type: "png",
Width: 400, Width: 400,
Height: 300, Height: 300,
OriginalUrl: "https://example.com/subitem.png", OriginalURL: "https://example.com/subitem.png",
CrushedKey: "crushed-123", CrushedKey: "crushed-123",
UseCrushedKey: true, UseCrushedKey: true,
}, },
@ -305,7 +304,7 @@ func TestMedia_JSONMarshalUnmarshal(t *testing.T) {
Type: "jpeg", Type: "jpeg",
Width: 1200, Width: 1200,
Height: 800, Height: 800,
OriginalUrl: "https://example.com/media.jpg", OriginalURL: "https://example.com/media.jpg",
CrushedKey: "crushed-media", CrushedKey: "crushed-media",
UseCrushedKey: false, UseCrushedKey: false,
}, },
@ -336,7 +335,7 @@ func TestMedia_JSONMarshalUnmarshal(t *testing.T) {
Poster: "https://example.com/poster.jpg", Poster: "https://example.com/poster.jpg",
Thumbnail: "https://example.com/thumb.jpg", Thumbnail: "https://example.com/thumb.jpg",
InputKey: "input-123", InputKey: "input-123",
OriginalUrl: "https://example.com/original.mp4", OriginalURL: "https://example.com/original.mp4",
}, },
} }
@ -363,7 +362,7 @@ func TestImageMedia_JSONMarshalUnmarshal(t *testing.T) {
Type: "gif", Type: "gif",
Width: 640, Width: 640,
Height: 480, Height: 480,
OriginalUrl: "https://example.com/image.gif", OriginalURL: "https://example.com/image.gif",
CrushedKey: "crushed-gif", CrushedKey: "crushed-gif",
UseCrushedKey: true, UseCrushedKey: true,
} }
@ -397,7 +396,7 @@ func TestVideoMedia_JSONMarshalUnmarshal(t *testing.T) {
Poster: "https://example.com/poster.jpg", Poster: "https://example.com/poster.jpg",
Thumbnail: "https://example.com/thumbnail.jpg", Thumbnail: "https://example.com/thumbnail.jpg",
InputKey: "upload-456", InputKey: "upload-456",
OriginalUrl: "https://example.com/original.webm", OriginalURL: "https://example.com/original.webm",
} }
// Marshal to JSON // Marshal to JSON

View File

@ -1,4 +1,3 @@
// Package services_test provides tests for the services package.
package services package services
import ( import (
@ -217,10 +216,8 @@ func TestApp_ProcessCourseFromFile(t *testing.T) {
if !contains(err.Error(), tt.expectedError) { if !contains(err.Error(), tt.expectedError) {
t.Errorf("Expected error containing '%s', got '%s'", tt.expectedError, err.Error()) t.Errorf("Expected error containing '%s', got '%s'", tt.expectedError, err.Error())
} }
} else { } else if err != nil {
if err != nil { t.Errorf("Expected no error, got: %v", err)
t.Errorf("Expected no error, got: %v", err)
}
} }
}) })
} }
@ -298,10 +295,8 @@ func TestApp_ProcessCourseFromURI(t *testing.T) {
if !contains(err.Error(), tt.expectedError) { if !contains(err.Error(), tt.expectedError) {
t.Errorf("Expected error containing '%s', got '%s'", tt.expectedError, err.Error()) t.Errorf("Expected error containing '%s', got '%s'", tt.expectedError, err.Error())
} }
} else { } else if err != nil {
if err != nil { t.Errorf("Expected no error, got: %v", err)
t.Errorf("Expected no error, got: %v", err)
}
} }
}) })
} }
@ -335,7 +330,7 @@ func TestApp_SupportedFormats(t *testing.T) {
// contains checks if a string contains a substring. // contains checks if a string contains a substring.
func contains(s, substr string) bool { func contains(s, substr string) bool {
return len(s) >= len(substr) && return len(s) >= len(substr) &&
(len(substr) == 0 || (substr == "" ||
s == substr || s == substr ||
(len(s) > len(substr) && (len(s) > len(substr) &&
(s[:len(substr)] == substr || (s[:len(substr)] == substr ||

View File

@ -1,5 +1,3 @@
// Package services provides the core functionality for the articulate-parser application.
// It implements the interfaces defined in the interfaces package.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package services_test provides tests for the HTML cleaner service.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package services provides the core functionality for the articulate-parser application.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package services_test provides benchmarks for the logger implementations.
package services package services
import ( import (

View File

@ -1,5 +1,3 @@
// Package services provides the core functionality for the articulate-parser application.
// It implements the interfaces defined in the interfaces package.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package services_test provides benchmarks for the parser service.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package services_test provides context-aware tests for the parser service.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package services_test provides tests for the parser service.
package services package services
import ( import (

View File

@ -1,4 +1,3 @@
// Package main_test provides tests for the main package utility functions.
package main package main
import ( import (