Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kudrykv committed Nov 2, 2024
1 parent 8cc3dcf commit 0f8685e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
24 changes: 3 additions & 21 deletions lib/latex_yearly_planner/xtypst/little_calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions lib/latex_yearly_planner/xtypst/little_calendar_row.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0f8685e

Please sign in to comment.