agenda-libre-ruby/app/helpers/events_helper.rb

86 lines
2.6 KiB
Ruby
Raw Normal View History

# Helper for the event views
2013-12-28 23:45:13 +01:00
module EventsHelper
2019-06-25 22:39:57 +02:00
def events_meta
set_meta_tags \
description: t('layouts.application.subtitle'),
keywords: params[:tag],
DC: {
title: t('layouts.application.title'),
subject: t('layouts.application.subtitle'),
publisher: 'april'
}
end
2019-06-25 22:39:57 +02:00
def event_meta(event)
set_meta_tags \
2019-06-25 22:27:24 +02:00
keywords: event.tag_list,
DC: { title: event.title, date: event.start_time.to_s },
geo: {
2019-06-25 22:27:24 +02:00
placename: event.city,
region: event.region,
position: "#{event.latitude};#{event.longitude}",
ICBM: "#{event.latitude}, #{event.longitude}"
}
end
2019-06-25 22:39:57 +02:00
def display_date(event)
2014-09-02 01:02:51 +02:00
if event.start_time.to_date == event.end_time.to_date
display_sameday event
else
display_multi_days event
end
end
def display_sameday(event)
t 'date.formats.same_day',
date: l(event.start_time.to_date, format: :at),
start: l(event.start_time, format: :hours),
end: l(event.end_time, format: :hours)
end
def display_multi_days(event)
t 'date.formats.period',
start: l(event.start_time, format: :at),
end: l(event.end_time, format: :at)
end
# Select the events to display in a month, sorted
def month_events(events, date)
events.select { |e| (e.start_time.to_date..e.end_time.to_date).cover? date }
.sort_by(&:city)
end
def display_attr(item, label, value = item[label])
# return unless value
item.class.human_attribute_name(label).rjust(12) + " #{value}"
end
2017-06-06 23:08:10 +02:00
# Using kramdown, let's parse the html and render it as markdown text
# No idea why, but also needs to remove extraneous quote encoding :(
def to_markdown(desc, line_width = -1)
return '' if desc.blank?
Kramdown::Document.new(spaces(desc), input: :html, line_width: line_width)
2017-06-10 14:28:10 +02:00
.to_kramdown
.gsub(/^#+\s+(.*)/, '**\1**')
.gsub(/\*\*\*\*/, '**')
2017-06-10 14:28:10 +02:00
.gsub(/\\([\"'])/, '\1') # Remove slash before quotes
.remove(/[[:blank:]]+$/) # Remove extraneous spaces
.remove(/{::}/) # Markdown artefact
end
private
# Cleaner html elements, to correct things like <em> test</em>
def spaces(desc)
desc = sanitize desc,
tags: %w[p br h1 h2 h3 h4 table tr th td ul ol li a strong
b em i sub sup],
attributes: %w[href]
desc.gsub(/<(strong|em|b|i)> /, ' <\1>')
.gsub(%r{ </(strong|em|b|i)>}, '</\1> ')
.gsub(/[[:space:]]([,.])/, '\1')
end
2013-12-28 23:45:13 +01:00
end