mirror of
https://github.com/kjanat/articulate-parser.git
synced 2026-01-16 17:02:11 +01:00
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:
@ -3,6 +3,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"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.
|
||||
// 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.
|
||||
func (a *App) ProcessCourseFromURI(uri, format, outputPath string) error {
|
||||
course, err := a.parser.FetchCourse(uri)
|
||||
func (a *App) ProcessCourseFromURI(ctx context.Context, uri, format, outputPath string) error {
|
||||
course, err := a.parser.FetchCourse(ctx, uri)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (a *App) GetSupportedFormats() []string {
|
||||
return a.exporterFactory.GetSupportedFormats()
|
||||
func (a *App) SupportedFormats() []string {
|
||||
return a.exporterFactory.SupportedFormats()
|
||||
}
|
||||
|
||||
@ -31,8 +31,8 @@ func (m *MockCourseParser) LoadCourseFromFile(filePath string) (*models.Course,
|
||||
|
||||
// MockExporter is a mock implementation of interfaces.Exporter for testing.
|
||||
type MockExporter struct {
|
||||
mockExport func(course *models.Course, outputPath string) error
|
||||
mockGetSupportedFormat func() string
|
||||
mockExport func(course *models.Course, outputPath string) error
|
||||
mockSupportedFormat func() string
|
||||
}
|
||||
|
||||
func (m *MockExporter) Export(course *models.Course, outputPath string) error {
|
||||
@ -42,17 +42,17 @@ func (m *MockExporter) Export(course *models.Course, outputPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockExporter) GetSupportedFormat() string {
|
||||
if m.mockGetSupportedFormat != nil {
|
||||
return m.mockGetSupportedFormat()
|
||||
func (m *MockExporter) SupportedFormat() string {
|
||||
if m.mockSupportedFormat != nil {
|
||||
return m.mockSupportedFormat()
|
||||
}
|
||||
return "mock"
|
||||
}
|
||||
|
||||
// MockExporterFactory is a mock implementation of interfaces.ExporterFactory for testing.
|
||||
type MockExporterFactory struct {
|
||||
mockCreateExporter func(format string) (*MockExporter, error)
|
||||
mockGetSupportedFormats func() []string
|
||||
mockCreateExporter func(format string) (*MockExporter, error)
|
||||
mockSupportedFormats func() []string
|
||||
}
|
||||
|
||||
func (m *MockExporterFactory) CreateExporter(format string) (interfaces.Exporter, error) {
|
||||
@ -63,9 +63,9 @@ func (m *MockExporterFactory) CreateExporter(format string) (interfaces.Exporter
|
||||
return &MockExporter{}, nil
|
||||
}
|
||||
|
||||
func (m *MockExporterFactory) GetSupportedFormats() []string {
|
||||
if m.mockGetSupportedFormats != nil {
|
||||
return m.mockGetSupportedFormats()
|
||||
func (m *MockExporterFactory) SupportedFormats() []string {
|
||||
if m.mockSupportedFormats != nil {
|
||||
return m.mockSupportedFormats()
|
||||
}
|
||||
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)
|
||||
formats := app.GetSupportedFormats()
|
||||
formats := app.SupportedFormats()
|
||||
if len(formats) == 0 {
|
||||
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.
|
||||
func TestApp_GetSupportedFormats(t *testing.T) {
|
||||
// TestApp_SupportedFormats tests the SupportedFormats method.
|
||||
func TestApp_SupportedFormats(t *testing.T) {
|
||||
expectedFormats := []string{"markdown", "docx", "pdf"}
|
||||
|
||||
parser := &MockCourseParser{}
|
||||
factory := &MockExporterFactory{
|
||||
mockGetSupportedFormats: func() []string {
|
||||
mockSupportedFormats: func() []string {
|
||||
return expectedFormats
|
||||
},
|
||||
}
|
||||
|
||||
app := NewApp(parser, factory)
|
||||
formats := app.GetSupportedFormats()
|
||||
formats := app.SupportedFormats()
|
||||
|
||||
if len(formats) != len(expectedFormats) {
|
||||
t.Errorf("Expected %d formats, got %d", len(expectedFormats), len(formats))
|
||||
|
||||
@ -7,6 +7,13 @@ import (
|
||||
"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.
|
||||
// It removes HTML tags while preserving their content and converts HTML entities
|
||||
// to their plain text equivalents.
|
||||
@ -30,8 +37,7 @@ func NewHTMLCleaner() *HTMLCleaner {
|
||||
// - A plain text string with all HTML elements and entities removed/converted
|
||||
func (h *HTMLCleaner) CleanHTML(html string) string {
|
||||
// Remove HTML tags but preserve content
|
||||
re := regexp.MustCompile(`<[^>]*>`)
|
||||
cleaned := re.ReplaceAllString(html, "")
|
||||
cleaned := htmlTagRegex.ReplaceAllString(html, "")
|
||||
|
||||
// Replace common HTML entities with their character equivalents
|
||||
cleaned = strings.ReplaceAll(cleaned, " ", " ")
|
||||
@ -46,7 +52,7 @@ func (h *HTMLCleaner) CleanHTML(html string) string {
|
||||
|
||||
// Clean up extra whitespace by replacing multiple spaces, tabs, and newlines
|
||||
// with a single space, then trim any leading/trailing whitespace
|
||||
cleaned = regexp.MustCompile(`\s+`).ReplaceAllString(cleaned, " ")
|
||||
cleaned = whitespaceRegex.ReplaceAllString(cleaned, " ")
|
||||
cleaned = strings.TrimSpace(cleaned)
|
||||
|
||||
return cleaned
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -42,13 +43,14 @@ func NewArticulateParser() interfaces.CourseParser {
|
||||
// The course data is then unmarshalled into a Course model.
|
||||
//
|
||||
// Parameters:
|
||||
// - ctx: Context for cancellation and timeout control
|
||||
// - uri: The Articulate Rise share URL (e.g., https://rise.articulate.com/share/SHARE_ID)
|
||||
//
|
||||
// Returns:
|
||||
// - A parsed Course model if successful
|
||||
// - An error if the fetch fails, if the share ID can't be extracted,
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -56,7 +58,12 @@ func (p *ArticulateParser) FetchCourse(uri string) (*models.Course, error) {
|
||||
|
||||
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 {
|
||||
return nil, fmt.Errorf("failed to fetch course data: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user