refactor: Standardize method names and introduce context propagation

Removes the `Get` prefix from exporter methods (e.g., GetSupportedFormat -> SupportedFormat) to better align with Go conventions for simple accessors.

Introduces `context.Context` propagation through the application, starting from `ProcessCourseFromURI` down to the HTTP request in the parser. This makes network operations cancellable and allows for setting deadlines, improving application robustness.

Additionally, optimizes the HTML cleaner by pre-compiling regular expressions for a minor performance gain.
This commit is contained in:
2025-11-06 04:25:54 +01:00
parent 65469ea52e
commit 2790064ad5
16 changed files with 90 additions and 69 deletions

2
.gitignore vendored
View File

@ -73,3 +73,5 @@ main_coverage
.task/ .task/
**/*.local.* **/*.local.*
.claude/

View File

@ -191,10 +191,10 @@ func (e *DocxExporter) exportSubItem(doc *docx.Docx, subItem *models.SubItem) {
} }
} }
// GetSupportedFormat returns the format name this exporter supports. // SupportedFormat returns the format name this exporter supports.
// //
// Returns: // Returns:
// - A string representing the supported format ("docx") // - A string representing the supported format ("docx")
func (e *DocxExporter) GetSupportedFormat() string { func (e *DocxExporter) SupportedFormat() string {
return "docx" return "docx"
} }

View File

@ -30,13 +30,13 @@ func TestNewDocxExporter(t *testing.T) {
} }
} }
// TestDocxExporter_GetSupportedFormat tests the GetSupportedFormat method. // TestDocxExporter_SupportedFormat tests the SupportedFormat method.
func TestDocxExporter_GetSupportedFormat(t *testing.T) { func TestDocxExporter_SupportedFormat(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner() htmlCleaner := services.NewHTMLCleaner()
exporter := NewDocxExporter(htmlCleaner) exporter := NewDocxExporter(htmlCleaner)
expected := "docx" expected := "docx"
result := exporter.GetSupportedFormat() result := exporter.SupportedFormat()
if result != expected { if result != expected {
t.Errorf("Expected format '%s', got '%s'", expected, result) t.Errorf("Expected format '%s', got '%s'", expected, result)

View File

@ -55,11 +55,11 @@ func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) {
} }
} }
// GetSupportedFormats returns a list of all supported export formats. // SupportedFormats returns a list of all supported export formats.
// This includes both primary format names and their aliases. // This includes both primary format names and their aliases.
// //
// Returns: // Returns:
// - A string slice containing all supported format names // - A string slice containing all supported format names
func (f *Factory) GetSupportedFormats() []string { func (f *Factory) SupportedFormats() []string {
return []string{"markdown", "md", "docx", "word", "html", "htm"} return []string{"markdown", "md", "docx", "word", "html", "htm"}
} }

View File

@ -125,7 +125,7 @@ func TestFactory_CreateExporter(t *testing.T) {
} }
// Check supported format // Check supported format
supportedFormat := exporter.GetSupportedFormat() supportedFormat := exporter.SupportedFormat()
if supportedFormat != tc.expectedFormat { if supportedFormat != tc.expectedFormat {
t.Errorf("Expected supported format '%s' for format '%s', got '%s'", tc.expectedFormat, tc.format, supportedFormat) t.Errorf("Expected supported format '%s' for format '%s', got '%s'", tc.expectedFormat, tc.format, supportedFormat)
} }
@ -173,7 +173,7 @@ func TestFactory_CreateExporter_CaseInsensitive(t *testing.T) {
t.Fatalf("CreateExporter returned nil for format '%s'", tc.format) t.Fatalf("CreateExporter returned nil for format '%s'", tc.format)
} }
supportedFormat := exporter.GetSupportedFormat() supportedFormat := exporter.SupportedFormat()
if supportedFormat != tc.expectedFormat { if supportedFormat != tc.expectedFormat {
t.Errorf("Expected supported format '%s' for format '%s', got '%s'", tc.expectedFormat, tc.format, supportedFormat) t.Errorf("Expected supported format '%s' for format '%s', got '%s'", tc.expectedFormat, tc.format, supportedFormat)
} }
@ -221,15 +221,15 @@ func TestFactory_CreateExporter_ErrorMessages(t *testing.T) {
} }
} }
// TestFactory_GetSupportedFormats tests the GetSupportedFormats method. // TestFactory_SupportedFormats tests the SupportedFormats method.
func TestFactory_GetSupportedFormats(t *testing.T) { func TestFactory_SupportedFormats(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner() htmlCleaner := services.NewHTMLCleaner()
factory := NewFactory(htmlCleaner) factory := NewFactory(htmlCleaner)
formats := factory.GetSupportedFormats() formats := factory.SupportedFormats()
if formats == nil { if formats == nil {
t.Fatal("GetSupportedFormats() returned nil") t.Fatal("SupportedFormats() returned nil")
} }
expected := []string{"markdown", "md", "docx", "word", "html", "htm"} expected := []string{"markdown", "md", "docx", "word", "html", "htm"}
@ -246,22 +246,22 @@ func TestFactory_GetSupportedFormats(t *testing.T) {
for _, format := range formats { for _, format := range formats {
exporter, err := factory.CreateExporter(format) exporter, err := factory.CreateExporter(format)
if err != nil { if err != nil {
t.Errorf("Format '%s' from GetSupportedFormats() should be creatable, got error: %v", format, err) t.Errorf("Format '%s' from SupportedFormats() should be creatable, got error: %v", format, err)
} }
if exporter == nil { if exporter == nil {
t.Errorf("Format '%s' from GetSupportedFormats() should create non-nil exporter", format) t.Errorf("Format '%s' from SupportedFormats() should create non-nil exporter", format)
} }
} }
} }
// TestFactory_GetSupportedFormats_Immutable tests that the returned slice is safe to modify. // TestFactory_SupportedFormats_Immutable tests that the returned slice is safe to modify.
func TestFactory_GetSupportedFormats_Immutable(t *testing.T) { func TestFactory_SupportedFormats_Immutable(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner() htmlCleaner := services.NewHTMLCleaner()
factory := NewFactory(htmlCleaner) factory := NewFactory(htmlCleaner)
// Get formats twice // Get formats twice
formats1 := factory.GetSupportedFormats() formats1 := factory.SupportedFormats()
formats2 := factory.GetSupportedFormats() formats2 := factory.SupportedFormats()
// Modify first slice // Modify first slice
if len(formats1) > 0 { if len(formats1) > 0 {
@ -270,13 +270,13 @@ func TestFactory_GetSupportedFormats_Immutable(t *testing.T) {
// Check that second call returns unmodified data // Check that second call returns unmodified data
if len(formats2) > 0 && formats2[0] == "modified" { if len(formats2) > 0 && formats2[0] == "modified" {
t.Error("GetSupportedFormats() should return independent slices") t.Error("SupportedFormats() should return independent slices")
} }
// Verify original functionality still works // Verify original functionality still works
formats3 := factory.GetSupportedFormats() formats3 := factory.SupportedFormats()
if len(formats3) == 0 { if len(formats3) == 0 {
t.Error("GetSupportedFormats() should still return formats after modification") t.Error("SupportedFormats() should still return formats after modification")
} }
} }
@ -436,7 +436,7 @@ func TestFactory_FormatNormalization(t *testing.T) {
t.Fatalf("Failed to create exporter for '%s': %v", tc.input, err) t.Fatalf("Failed to create exporter for '%s': %v", tc.input, err)
} }
format := exporter.GetSupportedFormat() format := exporter.SupportedFormat()
if format != tc.expected { if format != tc.expected {
t.Errorf("Expected format '%s' for input '%s', got '%s'", tc.expected, tc.input, format) t.Errorf("Expected format '%s' for input '%s', got '%s'", tc.expected, tc.input, format)
} }
@ -464,12 +464,12 @@ func BenchmarkFactory_CreateExporter_Docx(b *testing.B) {
} }
} }
// BenchmarkFactory_GetSupportedFormats benchmarks the GetSupportedFormats method. // BenchmarkFactory_SupportedFormats benchmarks the SupportedFormats method.
func BenchmarkFactory_GetSupportedFormats(b *testing.B) { func BenchmarkFactory_SupportedFormats(b *testing.B) {
htmlCleaner := services.NewHTMLCleaner() htmlCleaner := services.NewHTMLCleaner()
factory := NewFactory(htmlCleaner) factory := NewFactory(htmlCleaner)
for b.Loop() { for b.Loop() {
_ = factory.GetSupportedFormats() _ = factory.SupportedFormats()
} }
} }

View File

@ -113,12 +113,12 @@ func (e *HTMLExporter) Export(course *models.Course, outputPath string) error {
return os.WriteFile(outputPath, buf.Bytes(), 0644) return os.WriteFile(outputPath, buf.Bytes(), 0644)
} }
// GetSupportedFormat returns the format name this exporter supports // SupportedFormat returns the format name this exporter supports
// It indicates the file format that the HTMLExporter can generate. // It indicates the file format that the HTMLExporter can generate.
// //
// Returns: // Returns:
// - A string representing the supported format ("html") // - A string representing the supported format ("html")
func (e *HTMLExporter) GetSupportedFormat() string { func (e *HTMLExporter) SupportedFormat() string {
return "html" return "html"
} }

View File

@ -32,13 +32,13 @@ func TestNewHTMLExporter(t *testing.T) {
} }
} }
// TestHTMLExporter_GetSupportedFormat tests the GetSupportedFormat method. // TestHTMLExporter_SupportedFormat tests the SupportedFormat method.
func TestHTMLExporter_GetSupportedFormat(t *testing.T) { func TestHTMLExporter_SupportedFormat(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner() htmlCleaner := services.NewHTMLCleaner()
exporter := NewHTMLExporter(htmlCleaner) exporter := NewHTMLExporter(htmlCleaner)
expected := "html" expected := "html"
result := exporter.GetSupportedFormat() result := exporter.SupportedFormat()
if result != expected { if result != expected {
t.Errorf("Expected format '%s', got '%s'", expected, result) t.Errorf("Expected format '%s', got '%s'", expected, result)

View File

@ -92,12 +92,12 @@ func (e *MarkdownExporter) Export(course *models.Course, outputPath string) erro
return os.WriteFile(outputPath, buf.Bytes(), 0644) return os.WriteFile(outputPath, buf.Bytes(), 0644)
} }
// GetSupportedFormat returns the format name this exporter supports // SupportedFormat returns the format name this exporter supports
// It indicates the file format that the MarkdownExporter can generate. // It indicates the file format that the MarkdownExporter can generate.
// //
// Returns: // Returns:
// - A string representing the supported format ("markdown") // - A string representing the supported format ("markdown")
func (e *MarkdownExporter) GetSupportedFormat() string { func (e *MarkdownExporter) SupportedFormat() string {
return "markdown" return "markdown"
} }

View File

@ -32,13 +32,13 @@ func TestNewMarkdownExporter(t *testing.T) {
} }
} }
// TestMarkdownExporter_GetSupportedFormat tests the GetSupportedFormat method. // TestMarkdownExporter_SupportedFormat tests the SupportedFormat method.
func TestMarkdownExporter_GetSupportedFormat(t *testing.T) { func TestMarkdownExporter_SupportedFormat(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner() htmlCleaner := services.NewHTMLCleaner()
exporter := NewMarkdownExporter(htmlCleaner) exporter := NewMarkdownExporter(htmlCleaner)
expected := "markdown" expected := "markdown"
result := exporter.GetSupportedFormat() result := exporter.SupportedFormat()
if result != expected { if result != expected {
t.Errorf("Expected format '%s', got '%s'", expected, result) t.Errorf("Expected format '%s', got '%s'", expected, result)

View File

@ -12,9 +12,9 @@ type Exporter interface {
// specified output path. It returns an error if the export operation fails. // specified output path. It returns an error if the export operation fails.
Export(course *models.Course, outputPath string) error Export(course *models.Course, outputPath string) error
// GetSupportedFormat returns the name of the format this exporter supports. // SupportedFormat returns the name of the format this exporter supports.
// This is used to identify which exporter to use for a given format. // This is used to identify which exporter to use for a given format.
GetSupportedFormat() string SupportedFormat() string
} }
// ExporterFactory creates exporters for different formats. // ExporterFactory creates exporters for different formats.
@ -25,7 +25,7 @@ type ExporterFactory interface {
// It returns the appropriate exporter or an error if the format is not supported. // It returns the appropriate exporter or an error if the format is not supported.
CreateExporter(format string) (Exporter, error) CreateExporter(format string) (Exporter, error)
// GetSupportedFormats returns a list of all export formats supported by this factory. // SupportedFormats returns a list of all export formats supported by this factory.
// This is used to inform users of available export options. // This is used to inform users of available export options.
GetSupportedFormats() []string SupportedFormats() []string
} }

View File

@ -2,7 +2,11 @@
// It defines interfaces for parsing and exporting Articulate Rise courses. // It defines interfaces for parsing and exporting Articulate Rise courses.
package interfaces package interfaces
import "github.com/kjanat/articulate-parser/internal/models" import (
"context"
"github.com/kjanat/articulate-parser/internal/models"
)
// CourseParser defines the interface for loading course data. // CourseParser defines the interface for loading course data.
// It provides methods to fetch course content either from a remote URI // It provides methods to fetch course content either from a remote URI
@ -10,8 +14,9 @@ import "github.com/kjanat/articulate-parser/internal/models"
type CourseParser interface { type CourseParser interface {
// FetchCourse loads a course from a URI (typically an Articulate Rise share URL). // FetchCourse loads a course from a URI (typically an Articulate Rise share URL).
// It retrieves the course data from the remote location and returns a parsed Course model. // It retrieves the course data from the remote location and returns a parsed Course model.
// The context can be used for cancellation and timeout control.
// Returns an error if the fetch operation fails or if the data cannot be parsed. // Returns an error if the fetch operation fails or if the data cannot be parsed.
FetchCourse(uri string) (*models.Course, error) FetchCourse(ctx context.Context, uri string) (*models.Course, error)
// LoadCourseFromFile loads a course from a local file. // LoadCourseFromFile loads a course from a local file.
// It reads and parses the course data from the specified file path. // It reads and parses the course data from the specified file path.

View File

@ -3,6 +3,7 @@
package services package services
import ( import (
"context"
"fmt" "fmt"
"github.com/kjanat/articulate-parser/internal/interfaces" "github.com/kjanat/articulate-parser/internal/interfaces"
@ -44,8 +45,8 @@ func (a *App) ProcessCourseFromFile(filePath, format, outputPath string) error {
// ProcessCourseFromURI fetches a course from the provided URI and exports it to the specified format. // ProcessCourseFromURI fetches a course from the provided URI and exports it to the specified format.
// It takes the URI to fetch the course from, the desired export format, and the output file path. // It takes the URI to fetch the course from, the desired export format, and the output file path.
// Returns an error if fetching or exporting fails. // Returns an error if fetching or exporting fails.
func (a *App) ProcessCourseFromURI(uri, format, outputPath string) error { func (a *App) ProcessCourseFromURI(ctx context.Context, uri, format, outputPath string) error {
course, err := a.parser.FetchCourse(uri) course, err := a.parser.FetchCourse(ctx, uri)
if err != nil { if err != nil {
return fmt.Errorf("failed to fetch course: %w", err) return fmt.Errorf("failed to fetch course: %w", err)
} }
@ -69,8 +70,8 @@ func (a *App) exportCourse(course *models.Course, format, outputPath string) err
return nil return nil
} }
// GetSupportedFormats returns a list of all export formats supported by the application. // SupportedFormats returns a list of all export formats supported by the application.
// This information is provided by the ExporterFactory. // This information is provided by the ExporterFactory.
func (a *App) GetSupportedFormats() []string { func (a *App) SupportedFormats() []string {
return a.exporterFactory.GetSupportedFormats() return a.exporterFactory.SupportedFormats()
} }

View File

@ -32,7 +32,7 @@ func (m *MockCourseParser) LoadCourseFromFile(filePath string) (*models.Course,
// MockExporter is a mock implementation of interfaces.Exporter for testing. // MockExporter is a mock implementation of interfaces.Exporter for testing.
type MockExporter struct { type MockExporter struct {
mockExport func(course *models.Course, outputPath string) error mockExport func(course *models.Course, outputPath string) error
mockGetSupportedFormat func() string mockSupportedFormat func() string
} }
func (m *MockExporter) Export(course *models.Course, outputPath string) error { func (m *MockExporter) Export(course *models.Course, outputPath string) error {
@ -42,9 +42,9 @@ func (m *MockExporter) Export(course *models.Course, outputPath string) error {
return nil return nil
} }
func (m *MockExporter) GetSupportedFormat() string { func (m *MockExporter) SupportedFormat() string {
if m.mockGetSupportedFormat != nil { if m.mockSupportedFormat != nil {
return m.mockGetSupportedFormat() return m.mockSupportedFormat()
} }
return "mock" return "mock"
} }
@ -52,7 +52,7 @@ func (m *MockExporter) GetSupportedFormat() string {
// MockExporterFactory is a mock implementation of interfaces.ExporterFactory for testing. // MockExporterFactory is a mock implementation of interfaces.ExporterFactory for testing.
type MockExporterFactory struct { type MockExporterFactory struct {
mockCreateExporter func(format string) (*MockExporter, error) mockCreateExporter func(format string) (*MockExporter, error)
mockGetSupportedFormats func() []string mockSupportedFormats func() []string
} }
func (m *MockExporterFactory) CreateExporter(format string) (interfaces.Exporter, error) { func (m *MockExporterFactory) CreateExporter(format string) (interfaces.Exporter, error) {
@ -63,9 +63,9 @@ func (m *MockExporterFactory) CreateExporter(format string) (interfaces.Exporter
return &MockExporter{}, nil return &MockExporter{}, nil
} }
func (m *MockExporterFactory) GetSupportedFormats() []string { func (m *MockExporterFactory) SupportedFormats() []string {
if m.mockGetSupportedFormats != nil { if m.mockSupportedFormats != nil {
return m.mockGetSupportedFormats() return m.mockSupportedFormats()
} }
return []string{"mock"} return []string{"mock"}
} }
@ -119,7 +119,7 @@ func TestNewApp(t *testing.T) {
} }
// Test that the factory is set (we can't directly compare interface values) // Test that the factory is set (we can't directly compare interface values)
formats := app.GetSupportedFormats() formats := app.SupportedFormats()
if len(formats) == 0 { if len(formats) == 0 {
t.Error("App exporterFactory was not set correctly - no supported formats") t.Error("App exporterFactory was not set correctly - no supported formats")
} }
@ -306,19 +306,19 @@ func TestApp_ProcessCourseFromURI(t *testing.T) {
} }
} }
// TestApp_GetSupportedFormats tests the GetSupportedFormats method. // TestApp_SupportedFormats tests the SupportedFormats method.
func TestApp_GetSupportedFormats(t *testing.T) { func TestApp_SupportedFormats(t *testing.T) {
expectedFormats := []string{"markdown", "docx", "pdf"} expectedFormats := []string{"markdown", "docx", "pdf"}
parser := &MockCourseParser{} parser := &MockCourseParser{}
factory := &MockExporterFactory{ factory := &MockExporterFactory{
mockGetSupportedFormats: func() []string { mockSupportedFormats: func() []string {
return expectedFormats return expectedFormats
}, },
} }
app := NewApp(parser, factory) app := NewApp(parser, factory)
formats := app.GetSupportedFormats() formats := app.SupportedFormats()
if len(formats) != len(expectedFormats) { if len(formats) != len(expectedFormats) {
t.Errorf("Expected %d formats, got %d", len(expectedFormats), len(formats)) t.Errorf("Expected %d formats, got %d", len(expectedFormats), len(formats))

View File

@ -7,6 +7,13 @@ import (
"strings" "strings"
) )
var (
// htmlTagRegex matches HTML tags for removal
htmlTagRegex = regexp.MustCompile(`<[^>]*>`)
// whitespaceRegex matches multiple whitespace characters for normalization
whitespaceRegex = regexp.MustCompile(`\s+`)
)
// HTMLCleaner provides utilities for converting HTML content to plain text. // HTMLCleaner provides utilities for converting HTML content to plain text.
// It removes HTML tags while preserving their content and converts HTML entities // It removes HTML tags while preserving their content and converts HTML entities
// to their plain text equivalents. // to their plain text equivalents.
@ -30,8 +37,7 @@ func NewHTMLCleaner() *HTMLCleaner {
// - A plain text string with all HTML elements and entities removed/converted // - A plain text string with all HTML elements and entities removed/converted
func (h *HTMLCleaner) CleanHTML(html string) string { func (h *HTMLCleaner) CleanHTML(html string) string {
// Remove HTML tags but preserve content // Remove HTML tags but preserve content
re := regexp.MustCompile(`<[^>]*>`) cleaned := htmlTagRegex.ReplaceAllString(html, "")
cleaned := re.ReplaceAllString(html, "")
// Replace common HTML entities with their character equivalents // Replace common HTML entities with their character equivalents
cleaned = strings.ReplaceAll(cleaned, "&nbsp;", " ") cleaned = strings.ReplaceAll(cleaned, "&nbsp;", " ")
@ -46,7 +52,7 @@ func (h *HTMLCleaner) CleanHTML(html string) string {
// Clean up extra whitespace by replacing multiple spaces, tabs, and newlines // Clean up extra whitespace by replacing multiple spaces, tabs, and newlines
// with a single space, then trim any leading/trailing whitespace // with a single space, then trim any leading/trailing whitespace
cleaned = regexp.MustCompile(`\s+`).ReplaceAllString(cleaned, " ") cleaned = whitespaceRegex.ReplaceAllString(cleaned, " ")
cleaned = strings.TrimSpace(cleaned) cleaned = strings.TrimSpace(cleaned)
return cleaned return cleaned

View File

@ -3,6 +3,7 @@
package services package services
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -42,13 +43,14 @@ func NewArticulateParser() interfaces.CourseParser {
// The course data is then unmarshalled into a Course model. // The course data is then unmarshalled into a Course model.
// //
// Parameters: // Parameters:
// - ctx: Context for cancellation and timeout control
// - uri: The Articulate Rise share URL (e.g., https://rise.articulate.com/share/SHARE_ID) // - uri: The Articulate Rise share URL (e.g., https://rise.articulate.com/share/SHARE_ID)
// //
// Returns: // Returns:
// - A parsed Course model if successful // - A parsed Course model if successful
// - An error if the fetch fails, if the share ID can't be extracted, // - An error if the fetch fails, if the share ID can't be extracted,
// or if the response can't be parsed // or if the response can't be parsed
func (p *ArticulateParser) FetchCourse(uri string) (*models.Course, error) { func (p *ArticulateParser) FetchCourse(ctx context.Context, uri string) (*models.Course, error) {
shareID, err := p.extractShareID(uri) shareID, err := p.extractShareID(uri)
if err != nil { if err != nil {
return nil, err return nil, err
@ -56,7 +58,12 @@ func (p *ArticulateParser) FetchCourse(uri string) (*models.Course, error) {
apiURL := p.buildAPIURL(shareID) apiURL := p.buildAPIURL(shareID)
resp, err := p.Client.Get(apiURL) req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := p.Client.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to fetch course data: %w", err) return nil, fmt.Errorf("failed to fetch course data: %w", err)
} }

View File

@ -40,13 +40,13 @@ func run(args []string) int {
// Check for help flag // Check for help flag
if len(args) > 1 && (args[1] == "--help" || args[1] == "-h" || args[1] == "help") { if len(args) > 1 && (args[1] == "--help" || args[1] == "-h" || args[1] == "help") {
printUsage(args[0], app.GetSupportedFormats()) printUsage(args[0], app.SupportedFormats())
return 0 return 0
} }
// Check for required command-line arguments // Check for required command-line arguments
if len(args) < 4 { if len(args) < 4 {
printUsage(args[0], app.GetSupportedFormats()) printUsage(args[0], app.SupportedFormats())
return 1 return 1
} }