Skip to content

Instantly share code, notes, and snippets.

@ckozus
Last active August 29, 2015 14:21
Show Gist options
  • Save ckozus/4ee8e5a2479ad6b93bd8 to your computer and use it in GitHub Desktop.
Save ckozus/4ee8e5a2479ad6b93bd8 to your computer and use it in GitHub Desktop.
namespace :test do
task :parsing do
article_text = <<-eos
;ljaf dsf
asflkj asd;lkfj
sdflkjasf;ajksf
;lkadjsf;asdjf
{{calendar 2015-06-26 2015-06-28}}
aqui algo mas de texto
y despues el otro calendario
{{calendar 2015-07-04 2015-07-04 2015-07-04}}
eos
RE = /\{\{calendar(.*?)\}\}/
def parse_re(text)
text.gsub(RE) do |s|
fechas = $1.split(' ')
dates = fechas.map{|f| Date.parse(f)}
dates_by_month = dates.sort.group_by{|date| date.month}
rtn = ''
dates_by_month.each do |month, dates|
rtn << <<-eos
<div id='calendar_#{month}' class='article_calendar'>
#{dates.map{|d| "<span>#{d.to_s(:db)}</span>" }.join}
</div>
eos
end
rtn
end
end
puts parse_re(article_text)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment