I have a large text file that at some point looks like this :
<w>
<randomnode>
<pa/>
<pa>
Keyword1 sometxt1 <thing>abc</thing>: blabla
</pa>
<stuff>abc</stuff>
<pa>
just blabla
</pa>
<pa>
Keyword2 othertxt2: blabla
</pa>
<pa>
just blabla
</pa>
<pa>
just blabla
</pa>
<pa>
Keyword1 xxx: and blabla
</pa>
</randomnode>
</w>
and want to get this result:
<w>
<randomnode>
<k attr="keyword1 sometxt1">
<p>
<s>
Keyword1 sometxt1 <thing>abc</thing>:
</s>
blabla
</p>
<stuff>abc</stuff>
<p>
just blabla
</p>
</k>
<k attr="keyword1 othertxt2">
<p>
<s>
Keyword2 othertxt2:
</s>
blabla
</p>
<p>
just blabla
</p>
<p>
just blabla
</p>
</k>
<k attr="keyword1 xxx">
<p>
<s>
Keyword1 xxx:
</s>
and blabla
</p>
</k>
</randomnode>
</w>
In English: I want to go through each <pa> and group them whenever there is a keyword1 or keyword2 or keyword3 in the text() of that node.
The splitting-up for the content of the <s> on the : is done in another template and should work once I can group the <pa> correctly.
I have this so far:
<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']"
group-starting-with="pa/text()[contains(., 'keyword1')
or contains(., 'keyword2') or contains(., 'keyword3')]">
The problem ist that nothing is selected and I have a feeling it is because of text()...
Can I use group-starting-woth on text() at all? I would really like to use this and extend/correct it before I do something completly different..
Well I would ditch the text() completely and simply compare
<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']"
group-starting-with="pa[contains(., 'keyword1')
or contains(., 'keyword2') or contains(., 'keyword3')]">
If you want to use the text() child node selection then you need
<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']"
group-starting-with="pa[text()[contains(., 'keyword1')
or contains(., 'keyword2') or contains(., 'keyword3')]]">
Related
I'm using Hugo for the first time and I'm having troubles with translations.
More specifically, I'm using the hugo-theme-bootstrap theme. In config\_default, I have a file named author.toml that contains information like the author's name, bio, city, etc. It looks like this:
name = "Pascal Bergeron"
avatar = "images/profile.jpg"
bio = "Description française."
location = "Montréal"
The issue is that this file is used for both the English and French versions of my website. However, I need the bio and location fields to change according to the language. I've tried creating a file named author.fr.toml, but its settings end up being ignored. In fact, if I even rename author.toml to author.en.toml, the settings will be ignored for the English language. It's as if I can only have a author.toml file. This is strange because I can translate all other files in my config folder that way (I have a params.en.toml and a params.fr.toml file for instance).
I've done some digging in the layouts folder of my theme and I've found the file where the author.toml is used to create the HTML code. It looks like this:
{{- with .Site.Author -}}
<section class="profile surface row">
<div class="col-xl-6 d-flex align-items-center justify-content-center">
<img class="profile-avatar img-fluid" src="{{ absURL (default "images/profile.webp" .avatar) }}" alt="{{ .name }}" loading="lazy">
</div>
<div class="col-xl-6">
<h5 class="profile-name my-2">{{ .name }}</h5>
{{- with .bio -}}
<div class="profile-bio mb-2">{{ . }}</div>
{{- end -}}
{{- with .company -}}
<div class="profile-company mb-2"><i class="fas fa-fw fa-building"></i>{{ . }}</div>
{{- end -}}
{{- with .location -}}
<div class="profile-location mb-2"><i class="fas fa-fw fa-map-marker-alt"></i>{{ . }}</div>
{{- end -}}
{{- if .about -}}
<div class="profile-about mb-2"><i class="fas fa-fw fa-info-circle"></i><a target="_blank" href="{{ .about }}">{{ i18n "about_me" }}</a></div>
{{- else -}}
{{- with $.GetPage "about" -}}
<div class="profile-about mb-2"><i class="fas fa-fw fa-info-circle"></i>{{ .Title }}</div>
{{- end -}}
{{- end -}}
</div>
</section>
{{- end -}}
How can I have a author.toml file for each language?
(TLDR - The site author set-up the author data as 1 config file - i.e. Theme is built for 1 author for the entire site).
Hello Pascal, so, really appreciated you took the time to answer me.
To clarify:
This isn't a "hugo" thing, but the way this chap built this theme.
You are going to have to modify his theme (or reach out to him). The specific file in question, as you point out, I believe is profile.html in sidebar:
{{- if .Site.Author -}}
{{- $layout := default "" .Site.Author.params.layout -}}
{{- if eq $layout "compact" -}}
{{- partial "sidebar/profile/compact" . -}}
{{- else -}}
{{- partial "sidebar/profile/default" . -}}
{{- end -}}
{{- end -}}
If you follow the various partials this calls, i.e. down the rabbit whole you will find an example (this is one of many), like you describe and quote in your question, it has the specific comment {{ - with .Site.Author - }} meaning, it's looking for exactly 1 file, with a specific name.
[https://gohugo.io/content-management/multilingual/][1]
covers how to set-up a hugo multilingual - and there is SUPPOSED to be 1 Config file per site (which has it's own meaning in Hugo - see above page)
Point being, this is a theme issue, and I would suggest asking the theme creator to adapt edit.
My suggestion would be if the theme creator isn't helpful - would be to remove the dependency for the TOML file, and simply reference a headless bundle which has the author data, and as the theme has i18n, integrate the two (see above link) so that their is a headless bundle per language.
I have email template in which I keep and use ruby variables:
### mailer/_body.html.slim
ruby:
start_day_number = #absence.starts_on.strftime('%d').to_i.ordinalize
end_day_number = #absence.ends_on.strftime('%d').to_i.ordinalize
start_date = #absence.starts_on.strftime('#{start_day_number} of %B (%A)')
end_date = #absence.ends_on.strftime('#{end_day_number} of %B (%A)')
type = #absence.type.capitalize
status = #absence.status.capitalize
p class="details"
| Starts: <b>#{start_date}</b><br />
| Ends: <b>#{end_date}</b><br />
| Type: <b>#{type}</b><br />
| Status: <b>#{status}<b><br />
Is there any more convenient and readable way to store these variables in another .slim file and pass them to the template?
Optimally, I'd like to have them stored in layouts directory like that:
### layouts/mailer.html.slim
doctype html
html
head
meta charset="utf-8"
css:
...
body
ruby:
start_date = #absence.starts_on.strftime('#{start_day_number} of %B (%A)')
end_date = #absence.ends_on.strftime('#{end_day_number} of %B (%A)')
...
== yield
..but it didn't work though.
Decorator pattern turned out to be exactly what I needed. Thanks Tom Lord!
I am trying to write a Jenkins SCM plugin.
I need to validate the parameters (Test1, Test2).
Test1 depends upon DIR field.
Test2 depends upon DIR and Test1 fields.
Class containing Test1 & Test2 is a repeatable property. Config.jelly is following `
<f:entry title="DIR" field="Dir">
<f:textbox />
</f:entry>
<f:entry title="Variable" field="var">
<f:textbox />
</f:entry>
<f:entry>
<f:repeatableProperty field="directories" noAddButton="true" minimum="1"/>
</f:entry>
`
and Config.jelly for directories is following
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="Test1" field="test1">
<f:textbox />
</f:entry>
<f:entry title="Test2" field="test2">
<f:textbox />
</f:entry>
<f:entry>
<div align="right">
<input type="button" value="${%Add more workareas}..." class="repeatable-add show-if-last"/>
<input type="button" value="${%Delete}" class="repeatable-delete show-if-not-only" style="margin-left: 1em;"/>
</div>
How can i get value of DIR field for validating Test1? I used
public FormValidation doCheckTest1(#QueryParameter String value, #QueryParameter String Dir)
but i am getting Dir as null.
I am able to get the value of dir using #RelativePath("..")
I have a legacy Rails app, that can generate docx file. It's using just xml template, not any gem. Template is written using ERB syntax.
The problem is that generated file is marked as "corrupted" by MS Office Word, though LibreOffice on Linux opens it flawlessly. However, after recovering MS Office Word seems to open file without any content losses too.
I paste full XML template on pastebin.
While debugging I found out, that without the block, starting on the line 602, everything works fine. So I can't get, what's wrong with that particular piece of XML. I'll paste it right here for convenience
<% [task[:design_front], task[:design_back]].compact.each do |img_data| %>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial" />
<w:noProof />
<w:sz w:val="18" />
<w:szCs w:val="18" />
<w:lang w:eastAsia="ru-RU" />
</w:rPr>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:height] * 7400 %>" />
<wp:effectExtent l="0" t="0" r="0" b="0" />
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="0" />
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPicPr>
<a:picLocks noChangeAspect="0" noChangeArrowheads="0" />
</pic:cNvPicPr>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="<%= img_data[:id] %>" cstate="print">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
</a:ext>
</a:extLst>
</a:blip>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:width] * 7400 %>" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
<a:noFill />
<a:ln>
<a:noFill />
</a:ln>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
<% end %>
I tried to compare recovered file with my file, but I didn't see any crucial differences. I don't have that diff right now, but I can reproduce it if necessary.
Can someone show me the way? :) What am I doing wrong?
UPDATE
I tried to make corrections, suggested by Martin P., but no luck. Here is a diff between my generated file and recovered version (recovered on the right)
As far as I see, you are missing two element and some attributes.
(1) The wp:inline needs to have a wp:docPr element containing an id, name, and descr attribute.
<wp:docPr id="<% id %>" name="<% picture_name %>" descr="<% full_file_path_to_the_picture %>"/>
(2) The pic:nvPicPr element needs to have a pic:cNvPr element containing the same attributes.
<pic:cNvPr id="<% id %>" name="<% picture_name %>" descr="<% full_file_path_to_the_picture %>"/>
Of course you have to insert the missing variables (<% .. %>).
Here I marked the line where to insert the elements using comments:
<% [task[:design_front], task[:design_back]].compact.each do |img_data| %>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial" />
<w:noProof />
<w:sz w:val="18" />
<w:szCs w:val="18" />
<w:lang w:eastAsia="ru-RU" />
</w:rPr>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:height] * 7400 %>" />
<wp:effectExtent l="0" t="0" r="0" b="0" />
<!-- insert wp:docPr here -->
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="0" />
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPicPr>
<!-- insert pic:cNvPr here -->
<a:picLocks noChangeAspect="0" noChangeArrowheads="0" />
</pic:cNvPicPr>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="<%= img_data[:id] %>" cstate="print">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
</a:ext>
</a:extLst>
</a:blip>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="<%= img_data[:width] * 7400 %>" cy="<%= img_data[:width] * 7400 %>" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
<a:noFill />
<a:ln>
<a:noFill />
</a:ln>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
<% end %>
If you look at your diff, you may see those elements added.
The fact that MS Office Word replaced the value of r:embed suggests that there was no definition of #Id="image_1" in the relationships file of this document. The relevant relationships file is probably word/_rels/document.xml.rels.
After many hours of debugging the answer is found.
The last part of this puzzle was the file [Content_Types].xml. It contained the line <Default Extension="jpeg" ContentType="image/jpeg" />, but my images have .jpg extension. I changed Extension attribute to jpg and error was gone.
However, the additions suggested by Martin P. are necessary too (how can I credit him by the way?), because without them the resulting file remains corrupted, but with an another error message.
Thanks to everybody, who tried to help me. I hope, this answer will help someone in the future.
The HTML string is:
"<div>\r\n<video controls=\"controls\" height=\"313\" id=\"video201643154436\" poster=\"/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg\" width=\"500\"><source src=\"/uploads/ckeditor/attachments/23/newtons_law.mp4\" type=\"video/mp4\" />Your browser doesn't support video.<br />\r\nPlease download the file: video/mp4</video>\r\n</div>\r\n\r\n<div>test description</div>\r\n\r\n<div>\r\n<div>\r\n<video controls=\"controls\" height=\"300\" id=\"video201644152011\" poster=\"\" width=\"400\"><source src=\"/uploads/ckeditor/attachments/24/test.mp4\" type=\"video/mp4\" />Your browser doesn't support video.<br />\r\nPlease download the file: video/mp4</video>\r\n</div>\r\n\r\n<p> </p>\r\n</div>\r\n"
I want to replace all video tags including its content and sub tags with [[ Video ]]
The expected output is:
"<div>\r\n[[ Video ]]\r\n</div>\r\n\r\n<div>test description</div>\r\n\r\n<div>\r\n<div>\r\n[[ Video ]]\r\n</div>\r\n\r\n<p> </p>\r\n</div>\r\n"
I have tried using the regex /<video\s(.*?)<\/video(?=[>])>/, but it's not working properly.
I think that you need to substitute this two exact strings, and also the content inside this tags
first the beginning and ending strings:
"<video "
"</video>"
puts html_text.gsub("<video ","[[ video ]] ").gsub('</video>',"[[ video ]]")
This should work
irb(main):020:0> <div>
[[ video ]] controls="controls" height="313" id="video201643154436" poster="/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg" width="500"><source src="/uploads/ckeditor/attachments/23/newtons_law.mp4" type="video/mp4" />Your browser doesn't support video.<br />
Please download the file: video/mp4[[ video ]]
</div>
<div>test description</div>
<div>
<div>
[[ video ]] controls="controls" height="300" id="video201644152011" poster="" width="400"><source src="/uploads/ckeditor/attachments/24/test.mp4" type="video/mp4" />Your browser doesn't support video.<br />
Please download the file: video/mp4[[ video ]]
</div>
<p> </p>
</div>
=> true
or with regular expressions
puts html_text.gsub(/<\/?video[\s>]/, "[[ video ]]")
<div>
[[ video ]]controls="controls" height="313" id="video201643154436" poster="/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg" width="500"><source src="/uploads/ckeditor/attachments/23/newtons_law.mp4" type="video/mp4" />Your browser doesn't support video.<br />
Please download the file: video/mp4[[ video ]]
</div>
<div>test description</div>
<div>
<div>
[[ video ]]controls="controls" height="300" id="video201644152011" poster="" width="400"><source src="/uploads/ckeditor/attachments/24/test.mp4" type="video/mp4" />Your browser doesn't support video.<br />
Please download the file: video/mp4[[ video ]]
</div>
<p> </p>
</div>
Finally to remove all the inside this tag and all the content replace all. the problem is the \n character use this modifiers:
/.*/m multiline: . matches newline
/.*/i ignore case
/.*/x extended: ignore whitespace in pattern
so finally if we join alltogether the regular expression is:
puts html_text.gsub(/<video\s.*?<\/video>/mix, "[[ video ]]")
result
irb(main):043:0> <div>
[[ video ]]
</div>
<div>test description</div>
<div>
<div>
[[ video ]]
</div>
<p> </p>
</div>
=> true
Parsing html with regex very hard task to do. I'll suggest to use nokogiri or similar gem to parse it to ast and replace nodes you need.
anquegi's solution works perfectly. In the meantime, I tried nokogiri:
str = "<div>\r\n<video controls=\"controls\" height=\"313\" id=\"video201643154436\" poster=\"/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg\" width=\"500\"><source src=\"/uploads/ckeditor/attachments/23/newtons_law.mp4\" type=\"video/mp4\" />Your browser doesn't support video.<br />\r\nPlease download the file: video/mp4</video>\r\n</div>\r\n\r\n<div>test description</div>\r\n\r\n<div>\r\n<div>\r\n<video controls=\"controls\" height=\"300\" id=\"video201644152011\" poster=\"\" width=\"400\"><source src=\"/uploads/ckeditor/attachments/24/test.mp4\" type=\"video/mp4\" />Your browser doesn't support video.<br />\r\nPlease download the file: video/mp4</video>\r\n</div>\r\n\r\n<p> </p>\r\n</div>\r\n"
doc = Nokogiri::HTML(str)
doc.css("video").each do |video|
new_node = doc.create_element "p"
new_node.inner_html = "[[ Video ]]"
video.replace new_node
end
new_str = doc.css("body").to_s