From 0f8685e803e0c83e26518a21d4a74ffc0be3db61 Mon Sep 17 00:00:00 2001 From: Vitaliy Kudryk Date: Sat, 2 Nov 2024 20:45:57 +0200 Subject: [PATCH] refactor --- .../xtypst/little_calendar.rb | 24 +------- .../xtypst/little_calendar_row.rb | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 lib/latex_yearly_planner/xtypst/little_calendar_row.rb diff --git a/lib/latex_yearly_planner/xtypst/little_calendar.rb b/lib/latex_yearly_planner/xtypst/little_calendar.rb index ce488a68..94f40be8 100644 --- a/lib/latex_yearly_planner/xtypst/little_calendar.rb +++ b/lib/latex_yearly_planner/xtypst/little_calendar.rb @@ -76,27 +76,9 @@ def weekdays_row end def weeks - month.weeks.map(&method(:week_row)).join(",\n") - end - - def week_row(week) - row = week.days.map(&method(:map_day)) - return row.join(', ') unless parameters[:with_week_numbers] - - week_label = "[#{week.number}]" - week_label = "link(<#{week.id}>, #{week_label})" if parameters[:link_to_week] - - row.unshift(week_label) if parameters[:week_number_placement] == 'left' - row.push(week_label) if parameters[:week_number_placement] == 'right' - - row.join(', ') - end - - def map_day(day) - return '[]' unless day - return "link(<#{day.id}>, [#{day.day}])" if highlighted_day != day - - "table.cell(fill: black, link(<#{day.id}>, text(white)[#{day.day}]))" + month.weeks + .map { |week| LittleCalendarRow.new(week, **parameters).to_typst } + .join(",\n") end def highlighted_day diff --git a/lib/latex_yearly_planner/xtypst/little_calendar_row.rb b/lib/latex_yearly_planner/xtypst/little_calendar_row.rb new file mode 100644 index 00000000..6853263a --- /dev/null +++ b/lib/latex_yearly_planner/xtypst/little_calendar_row.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module LatexYearlyPlanner + module Xtypst + class LittleCalendarRow + DEFAULT_PARAMETERS = { + week_with_numbers: true, + link_to_week: true, + week_number_placement: 'left', + highlight_day: nil + }.freeze + + attr_reader :week, :parameters + + def initialize(week, **parameters) + @week = week + @parameters = DEFAULT_PARAMETERS.merge(parameters.compact) + end + + def to_typst + row.join(', ') + end + + private + + def row + return row_internal unless parameters[:week_with_numbers] + + add_week! + + row_internal + end + + def add_week! + return row_internal.unshift(week_label) if parameters[:week_number_placement] == 'left' + + row_internal.push(week_label) + end + + def week_label + return "[#{week.number}]" unless parameters[:link_to_week] + + "link(<#{week.id}>, [#{week.number}])" + end + + def row_internal + @row_internal ||= week.days.map(&method(:map_day)) + end + + def map_day(day) + return '[]' unless day + return "link(<#{day.id}>, [#{day.day}])" if parameters[:highlight_day] != day + + "table.cell(fill: black, link(<#{day.id}>, text(white)[#{day.day}]))" + end + end + end +end