auto fix template error

This commit is contained in:
BOYA,CHIU 2021-08-24 23:09:19 +08:00
parent ce1c565438
commit 24263734b1
5 changed files with 69 additions and 6 deletions

View File

@ -32,8 +32,10 @@ class EventNewsController < ApplicationController
"event_news_files" => files,
"title" => a.title,
"speaker" => a.speaker,
"speaker-css" => (a.speaker.blank? ? "display: none;" : ""),
"place" => a.place,
"host" => a.host,
"host-css" => (a.host.blank? ? "display: none;" : ""),
"notes" => a.notes,
"source-site" => "",
"source-site-title" => "",
@ -564,9 +566,9 @@ class EventNewsController < ApplicationController
"speaker-head" => EventNewsCustomTitle.get_trans('speaker'),
"host-head" => EventNewsCustomTitle.get_trans('host'),
"notes-head" => t('event_news.notes'),
"speaker-css" => (event_news.speaker.blank? ? "display: none;" : ""),
"host-css" => (event_news.host.blank? ? "display: none;" : ""),
"speaker" => event_news.speaker,
"speaker-css" => (event_news.speaker.blank? ? "display: none;" : "display: inline-block;"),
"host-css" => (event_news.host.blank? ? "display: none;" : "display: inline-block;"),
"host" => event_news.host,
"notes" => event_news.notes,
"title" => event_news.title,

View File

@ -5,6 +5,7 @@ $:.push File.expand_path("../lib", __FILE__)
require "event_news_mod/version"
bundle_update_flag = ARGV[0]=='update' || ARGV[0]=='install'
if bundle_update_flag
require File.expand_path("../update_event_news", __FILE__)
env_pwd = ENV['PWD']
app_path = File.expand_path(__dir__)
template_path = env_pwd + '/app/templates'
@ -15,6 +16,7 @@ if bundle_update_flag
info_json_file = "#{folder}modules/event_news/info.json"
if File.exist?(info_json_file)
Bundler.with_clean_env{system ('cp -f '+ app_path + '/modules/event_news/show.html.erb ' + "#{folder}modules/event_news/.")}
update_event_news_template(folder)
begin
file_text = File.read(info_json_file) rescue ""
encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???")

View File

@ -24,8 +24,8 @@
<a class="w-annc__title" href="{{link_to_show}}">{{title}}</a>
</h4>
<div class="w-annc__subtitle">{{subtitle}}</div>
<div class="w-annc__speaker">【{{speaker-head}}】 {{speaker}}</div>
<div class="w-annc__host">【{{host-head}}】 {{host}}</div>
<div class="w-annc__speaker" style="{{speaker-css}}">【{{speaker-head}}】 {{speaker}}</div>
<div class="w-annc__host" style="{{host-css}}">【{{host-head}}】 {{host}}</div>
</li>
</ul>
<div class="w-annc__more-wrap clearfix">

View File

@ -157,12 +157,12 @@
</span>
</li>
<li class="s-annc__extra_info-wrap s-annc__meta--item ">
<span style="{{speaker-css}}">
<span style="display: inline-block;{{speaker-css}}">
<span>{{speaker-head}}:</span>
<span>{{speaker}}</span>
<span>&nbsp;/&nbsp;</span>
</span>
<span style="{{host-css}}">
<span>&nbsp;/&nbsp;</span>
<span style="display: inline-block;">
<span>{{host-head}}:</span>
<span>{{host}}</span>

59
update_event_news.rb Normal file
View File

@ -0,0 +1,59 @@
def add_css_style_for_block(c,head_key,value_key,css_key)
#tmp = []
c_tp = c
flag = false
if !c.include?("{{#{css_key}}}")
c_tp = c.clone()
c.scan(/((?:【| )*{{#{head_key}}}((?:(?!<tr).)*){{#{value_key}}})/m) do |c1|
flag = true
s,e = $~.offset(0)
tag_gs = c1[1].scan(/<(\/)*(\w+)(?:(?!<|>).)*>/)
tag_gs.each do |tag_g|
if tag_g[0]
s = s - c[0...s].reverse.match(/>(?:(?!<|>).)*#{tag_g[1].reverse}</).offset(0)[1]
else
e = c[e..-1].match(/<\/#{tag_g[1]}(?:(?!<|>).)*>/).offset(0)[1] + e
end
end
inner_c = c[s...e]
wrap_tag = c[e..-1].match(/<\/(\w+)(?:(?!<|>).)*>/)
e = wrap_tag.offset(0)[1] + e
s = s - c[0...s].reverse.match(/>(?:(?!<|>).)*#{wrap_tag[1].reverse}</).offset(0)[1]
outer_c = c[s...e]
ext_vars = outer_c.scan(/{{((?:(?!}).)*)}}/).flatten-[head_key,value_key]
#tmp << [outer_c,inner_c,ext_vars]
if ext_vars.length==0
outer_c_new = outer_c.gsub(/(<\w+)((?:(?!>).)*)(>.*)/m) do |m|
v1 = $1
v2 = $2
v3 = $3
have_style_flag = false
v2 = v2.gsub(/(style=(?:\"|\'))((?:(?!\"|\').)*)((?:\"|\'))/) do |m2|
have_style_flag = true
"#{$1}#{$2}{{#{css_key}}}#{$3}"
end
if !have_style_flag
v2 = v2 + " style=\"{{#{css_key}}}\""
end
v1+v2+v3
end
c_tp = c_tp.sub(outer_c,outer_c_new)
else
c_tp = c_tp.sub(inner_c,"<span style=\"{{#{css_key}}}\">#{inner_c}<\/span>")
end
end
end
#[tmp,c_tp]
[flag,c_tp]
end
def update_event_news_template(folder)
Dir["#{folder}modules/event_news/*.html.erb"].each do |f|
c = File.open(f,'r'){|f1| f1.read}
flag1,c = add_css_style_for_block(c,'speaker-head','speaker','speaker-css')
flag2,c = add_css_style_for_block(c,'host-head','host','host-css')
if flag1 || flag2
puts ['update file:',f]
File.open(f,'w'){|f1| f1.write(c)}
end
end
end