This is an automated email from the ASF dual-hosted git repository. sebb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/whimsy.git
The following commit(s) were added to refs/heads/master by this push: new 13428d25 Simplify Mustache rendering 13428d25 is described below commit 13428d25741e79563aa40bb9b067ed22163927f4 Author: Sebb <s...@apache.org> AuthorDate: Fri Jan 5 23:04:42 2024 +0000 Simplify Mustache rendering --- www/board/agenda/helpers/mustache-template.rb | 55 ++++++++++++++++++++++ www/board/agenda/main.rb | 1 + .../agenda/views/actions/reminder-text.json.rb | 11 +---- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/www/board/agenda/helpers/mustache-template.rb b/www/board/agenda/helpers/mustache-template.rb new file mode 100644 index 00000000..dd61eb24 --- /dev/null +++ b/www/board/agenda/helpers/mustache-template.rb @@ -0,0 +1,55 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if __FILE__ == $0 # This is normally done by main.rb + require 'mustache' + FOUNDATION_BOARD = '/srv/svn/foundation_board' +end + +# simplify processing of Agenda Mustache templates +# params: +# template - the prefix name, e.g. reminder1 +# context - the variable values to be used +# raise_on_context_miss - raise an Exception if any variables are missing [default true] +# returns: {subject: subject, body: body} +class AgendaTemplate + def self.render(template, context, raise_on_context_miss=false) + unless template =~ /\A[-\w]+\z/ + raise ArgumentError.new("Invalid template name #{template}") + end + m = Mustache.new + m.template_file = File.join(FOUNDATION_BOARD, 'templates', template+'.mustache') + m.raise_on_context_miss = raise_on_context_miss + template = m.render(context) + # extract subject + subject = template[/Subject: (.*)/, 1] + template[/Subject: .*\s+/] = '' + + # return results + {subject: subject, body: template} + end +end + +if __FILE__ == $0 + sent_emails = [] + sent_emails << {name: 'a', emails: 'c, d'} + sent_emails << {name: 'a', emails: 'c, d'} + sent_emails << {name: 'a', emails: 'c, d'} + sent_emails << {name: 'a', emails: 'c, d'} + view = {meeting: 'meeting', agenda: 'board_agenda_2024_01_17.txt', unsent: ['a', 'b', 'c'], sent_emails: sent_emails} + render = AgendaTemplate.render('reminder-summary',view, true) + puts render[:subject] + puts render[:body] +end diff --git a/www/board/agenda/main.rb b/www/board/agenda/main.rb index 83086903..fbc8c7d4 100755 --- a/www/board/agenda/main.rb +++ b/www/board/agenda/main.rb @@ -55,6 +55,7 @@ require_relative './models/agenda' require_relative './models/minutes' require_relative './models/comments' require_relative './models/reporter' +require_relative './helpers/mustache-template' require_relative './helpers/string' require_relative './helpers/integer' require_relative './daemon/session' diff --git a/www/board/agenda/views/actions/reminder-text.json.rb b/www/board/agenda/views/actions/reminder-text.json.rb index b7e87a08..0a99db58 100644 --- a/www/board/agenda/views/actions/reminder-text.json.rb +++ b/www/board/agenda/views/actions/reminder-text.json.rb @@ -3,8 +3,6 @@ require 'active_support/time' raise ArgumentError, "Invalid syntax #{@reminder}" unless @reminder =~ /\A[-\w]+\z/ -# read template for the reminders -template = File.read(File.join(FOUNDATION_BOARD, 'templates', "#{@reminder}.mustache")) # Allow override of timeZoneInfo (avoids the need to parse the last agenda) timeZoneInfo = @tzlink @@ -32,11 +30,4 @@ view = { } # perform the substitution -template = Mustache.render(template, view) - -# extract subject -subject = template[/Subject: (.*)/, 1] -template[/Subject: .*\s+/] = '' - -# return results -{subject: subject, body: template} +AgendaTemplate.render(@reminder, view)