How to highlite blocks of HTML code in c++ code? - qt-creator

I write web project on c++. In my c++ code has to insert html, for example such
void CPage::putBaseFooter() {
if(m_canRender) {
HTML(
<!++
</main>
<footer>
<f++ composePageFooter(); ++f>
</footer>
</body>
</html>
++!>);
}
}
That is the whole html code is between <!++ and ++!> (Code is processed before compiling its own preprocessor to string)
Just have your own macros preprocessor, such as for example
<f++ composeHead(); ++f>
<v++ ts.tm_year + 1900++v>
<paged_list++ [day_tasks_control] [/tasks/list] [taskListRenderer]>
...
<++paged_list>
<labeled_control++ [Description] [taskDescription]>
<textarea></textarea>
<++labeled_control>
Tell me please, how i can highlite html keywords and own macroses into qt-creator code editor? I tried to write a higlite-xml for Kate (with inheritance c++ highlite), but probably something I do not understand, since the backlight does not work.
Here are my sketches syntax highlighting
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd"
[
<!ENTITY space " ">
<!ENTITY separators ",;">
<!ENTITY ns_punctuators "!%&space;&()+-/.*<=>?[]{|}~^&separators;">
]>
<!--
Copyright (c) 2012 by Alex Turbov (i.zaufi#gmail.com)
-->
<language
name="C++"
section="Sources"
version="1.0"
kateversion="2.4"
indenter="cstyle"
style="C++"
mimetype="text/x-c++src;text/x-c++hdr;text/x-chdr"
extensions="*.c++;*.cxx;*.cpp;*.cc;*.C;*.h;*.hh;*.H;*.h++;*.hxx;*.hpp;*.hcc;*.moc"
author="Sheridan (gorlov.maxim#gmail.com)"
license="LGPL"
priority="11"
>
<highlighting>
<list name="InplaceHTML">
<item> form </item>
<item> table </item>
<item> div </item>
<item> td </item>
<item> tr </item>
<item> th </item>
<item> span </item>
<item> input </item>
<item> textarea </item>
<item> label </item>
<item> a </item>
<item> head </item>
<item> link </item>
<item> script </item>
</list>
<contexts>
<context attribute="Normal Text" lineEndContext="#stay" name="Normal">
<IncludeRules context="##C++" />
<IncludeRules context="DetectInplaceHTML" />
</context>
<context attribute="Normal Text" lineEndContext="#stay" name="DetectInplaceHTML">
<keyword attribute="Inplace HTML" context="#stay" String="InplaceHTML" />
</context>
</contexts>
<itemDatas>
<itemData name="Normal Text" defStyleNum="dsNormal" spellChecking="false" />
<itemData name="Inplace HTML" defStyleNum="dsKeyword" color="#0095ff" selColor="#ffffff" bold="1" italic="0" spellChecking="false" />
</itemDatas>
</highlighting>
</language>

The Kate C++ highlighting is not used in Qt Creator, changing/extending the Kate configuration file won't have anny affect. You could try to register a separate mime type that's not recognized as C or C++ by Qt Creator and write a Kate highlighter for that, but I don't know whether this would work.

Actually you may have few highlighters for the same language, but w/ different priority!
Take a look to syntax files: there is ISO C++ and C++ (which is a pure C++ syntax plus Qt4 addons). Also here is alternative C++ highlighters where C++ is a pure C++ syntax and C++/Qt4 is a secondary. One may use configuration settings to change priority according needs. Personally I prefer to have a pure C++ over "default" C++/Qt4.
So you may try to add your own C++/Custom syntax and boost its priority. Take a look to C++/Qt4 to get an idea how to "reuse" pure C++ syntax.
And finally, considering your example syntax, you'd better to detect your extension before fall into inherited C++ contexts.

Related

Is there a way to find a specific string in an xml file and then replace the next string underneath it with a batch script?

Is this possible? I need to edit the following xml file. For every "BASerialKeyND", I need to replace the 987654321 right underneath it. Same thing for "BASerialKey" and 98-7654-321. I cannot count the lines in the file and assign it to a variable and then replace those specific lines because BASerialKeyND and BASerialKey occur on different lines in different files.
Thanks so much for your help!!!!!!!!!!
<?xml version="1.0" encoding="utf-8"?>
<SerializableDictionary>
<item>
<key>BASerialKeyND</key>
<value>987654321</value>
</item>
<item>
<key>BASerialKey</key>
<value>98-7654-321</value>
</item>
<item>
<key>MACHINETYPE</key>
<value>Max</value>
</item>
<item>
<key>PC1NAME</key>
<value>987654321PC1</value>
</item>
<item>
<key>PC2NAME</key>
<value>987654321PC2</value>
</item>
<item>
<key>REPORTPRINTER</key>
<value>None</value>
</item>
<item>
<key>PC1PRINTERS</key>
<value>Name=Microsoft XPS Document Writer
</SerializableDictionary>

extract element from xml using xmlstarlet

I'm trying to extract the "enclosure url" from the xml below using xmlstarlet.
I found this Extract values from XML using xmlstarlet which has given me a couple of pointers but I'm a little confused as to why ias=http://www.oracle.com/ias-instance is set. Shouldn't the element be referenced? not the content?
Could something similar be used against the xml below or am I barking up the wrong tree?
<rss version="2.0">
<channel>
<title>Wrestle Me</title>
<description>
</description>
<acast:showId>fba44d70-2a73-44d9-8bf9-726e9fcc2b3e</acast:showId>
<acast:showUrl>wrestleme</acast:showUrl>
<link>https://play.acast.com/s/wrestleme</link>
<atom:link rel="self" type="application/rss+xml" href="https://rss.acast.com/wrestleme"/>
<pingback:receiver>https://interaction.acast.com/pingback</pingback:receiver>
<lastBuildDate>Thu, 18 Jul 2019 04:05:27 GMT</lastBuildDate>
<pubDate>Thu, 18 Jul 2019 04:00:00 GMT</pubDate>
<ttl>30</ttl>
<language>en</language>
<copyright>
</copyright>
<docs>https://www.acast.com/wrestleme</docs>
<acast:signature key="EXAMPLE" algorithm="aes-256-cbc">9w1/WFWZb9YCoFYTxRGhxA==</acast:signature>
<acast:network>
</acast:network>
<acast:settings>
</acast:settings>
<image>
</image>
<itunes:image href="https://thumborcdn.acast.com/66RENnKTBsvtDbIhZez9wTij8hg=/1500x1500/https://mediacdn.acast.com/assets/fba44d70-2a73-44d9-8bf9-726e9fcc2b3e/-jbxjj7ek-whatsapp_image_2018-01-02_at_11-16-53.jpeg"/>
<itunes:subtitle>
</itunes:subtitle>
<itunes:type>episodic</itunes:type>
<itunes:author>Radio Stakhanov</itunes:author>
<itunes:summary></itunes:summary>
<itunes:owner>
</itunes:owner>
<itunes:explicit>yes</itunes:explicit>
<itunes:keywords/>
<itunes:category text="Sports & Recreation">
</itunes:category>
<media:credit role="author">Radio Stakhanov</media:credit>
<media:description type="html">
</media:description>
<item>
</item>
<item>
<title>It just makes me sad - WrestleMania XX part 4</title>
<acast:episodeId>b51a603d-8980-4cef-9a6c-b5cf0e5038a0</acast:episodeId>
<acast:episodeUrl>itjustmakesmesad-wrestlemaniaxxpart4</acast:episodeUrl>
<itunes:subtitle>
</itunes:subtitle>
<itunes:summary>
</itunes:summary>
<guid isPermaLink="false">b51a603d-8980-4cef-9a6c-b5cf0e5038a0</guid>
<pubDate>Thu, 11 Jul 2019 01:00:00 GMT</pubDate>
<itunes:duration>00:40:32</itunes:duration>
<itunes:keywords/>
<itunes:explicit>no</itunes:explicit>
<itunes:episodeType>full</itunes:episodeType>
<itunes:image href="https://thumborcdn.acast.com/iRcL017SnqZUc-ne7xmt6JnGZQw=/3000x3000/https://mediacdn.acast.com/assets/b51a603d-8980-4cef-9a6c-b5cf0e5038a0/cover-image-jwamv8ll-wrestleme_1500.jpeg"/>
<description>
</description>
<acast:settings>
</acast:settings>
<link>
</link>
<enclosure url="https://media.acast.com/wrestleme/itjustmakesmesad-wrestlemaniaxxpart4/media.mp3" length="38927363" type="audio/mpeg"/>
</item>
</channel>
</rss>

Parsing XPath Query

I would like to receive the output:
10.62.197.125, 10.62.197.126, 10.62.197.127.
How do I make that happen? (Note that the code has been somehow manipulated due to customer restrictions).
<output arrayType="ns2:aaa[20]" type="ns2:aaa_array">
<item type="ns2:bbb">
<ip_addr type="xsd:string">0a2ef556</ip_addr>
<hostaddr type="xsd:string">10.62.197.125</hostaddr>
<subnet_name type="xsd:string">Test</subnet_name>
<pool_id type="xsd:string">0</pool_id>
</item>
<item type="ns2:aaa">
<ip_addr type="xsd:string">0a3c3c33</ip_addr>
<hostaddr type="xsd:string">10.62.197.126</hostaddr>
<subnet_name type="xsd:string">Test</subnet_name>
<pool_id type="xsd:string">0</pool_id>
</item>
<item type="ns2:aaa">
<ip_addr type="xsd:string">0a3a72c4</ip_addr>
<hostaddr type="xsd:string">10.62.197.127</hostaddr>
<subnet_name type="xsd:string">Test</subnet_name>
<pool_id type="xsd:string">0</pool_id>
</item>
</output>
To get the elements:
/*/item/hostaddr/text()
results in
10.62.197.125
10.62.197.126
10.62.197.127
To get a string (supported by XPath 2.0+):
string-join(/*/item/hostaddr/text(), ', ')
results in
10.62.197.125, 10.62.197.126, 10.62.197.127
This means:
Select the text in the hostaddr element in all item elements.
Test:
You can test this by going to a XPath tester, like this, paste your XML in Option 1, paste the expression in XPath expression field and press the test XPath button.

Xpath Cast node to number for mod

I have nodes that contain numbers that I would like to cast to a number and use mod. For example:
<item>
<num>1</num>
</item>
<item>
<num>2</num>
</item>
<item>
<num>3</num>
</item>
I've tried:
num mod 3 -- returns NaN
number(num) mod 3 -- returns NaN
number(string(num)) -- returns NaN
Any idea if this can be done? Even if there was a way to convert to ASCII, I would take it
Thanks in advance!
number(num) mod 3 should work. The following example files output 1 2 0 as expected.
XML
(saved as input.xml)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mod_test.xsl"?>
<items>
<item>
<num>1</num>
</item>
<item>
<num>2</num>
</item>
<item>
<num>3</num>
</item>
</items>
XSL
(saved as mod_text.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="//item">
<xsl:value-of select="number(num) mod 3"/>
</xsl:template>
</xsl:stylesheet>
Note: just num mod 3 in the select also works.
For reference, here is the relevant section in the documentation.
I've tried:
num mod 3 -- returns NaN
number(num) mod 3 -- returns NaN
number(string(num)) -- returns NaN
Any idea if this can be done?
As no complete XML document is provided, here are my two guesses:
The context node for the relative expressions has no num children. The solution is to assure that the context node is the correct one, or to use absolute XPath expression(s).
The not-shown XML document is in a default namespace. In this case the solution is to "register a namespace" (associate a string-prefix to the default namespace, say "x") and then replace in your expressio(s) num with x:num.

Ruby zlib::Gzip not working properly

I was in the middle of creating an inport/export system that could encode a costume data structure in an xml tree and then read it back and recreate the object.
I got the xml part to work fine but when I discovered that the xml file was 1.5mb when the original ruby::Marshal file was only 105kb I decided that it would be a good idea to compress the file.
so I did this
require "rexml/document"
require "zlib"
include REXML
tilesetfile = File.new( "tilesets.rmpy", "w+" )
buffer = ""
tilesetgz = Zlib::GzipWriter.new(tilesetfile)
puts "Compressing output for: tilesets.rxdata ..."
tilesetdoc.write(buffer, 0)
tilesetgz.write(buffer)
tilesetgz.close
then I tried to get the buffer string back so that I could phrase it as xml again like so
require "rexml/document"
require "zlib"
include REXML
tilesetfile = File.open("tilesets.rmpy", "r")
tilesetgz = Zlib::GzipReader.new(tilesetfile)
testfile = File.new("importtest.txt", "w")
tilesetdoc = Document.new tilesetgz
it should be noted that neither of these spinets contains the entire system, just the require header and the last few lines that do the compression
but I get a phrasing error because the xml document has bee corrupted some how
this is the out put of the import scrupt before I Gziped it (tracated of course the file is 1.5mb after all)
<tilesetdata>
<tileset>
<id>
1
</id>
<022-Roof01/>
<tileset_name>
019-DesertTown01
</tileset_name>
<autotile_names>
<item>
015-Sa_Water01
</item>
<item>
016-Sa_Shadow01
</item>
<item>
018-Sa_Ground01
</item>
<item>
019-Sa_Grass02
</item>
<item>
020-Sa_Grass03
</item>
<item>
021-Sa_Road01
</item>
<item>
022-Roof01
</item>
</autotile_names>
<panorama_name>
</panorama_name>
<panorama_hue>
0
</panorama_hue>
<fog_name>
</fog_name>
<fog_hue>
0
</fog_hue>
<fog_opacity>
64
</fog_opacity>
<fog_blend_type>
0
</fog_blend_type>
<fog_zoom>
200
</fog_zoom>
<fog_sx>
0
</fog_sx>
<fog_sy>
0
</fog_sy>
<battleback_name>
</battleback_name>
<passages>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
on the import side I intercepted the the unGziped file strangely enough the file is 1.3mb this time
<tilesetdata>
<tileset>
<id>
1
</id>
<022-Roof01/>
<tileset_name>
019-DesertTown01
</tileset_name>
<autotile_names>
<item>
015-Sa_Water01
</item>
<item>
016-Sa_Shadow01
</item>
<item>
018-Sa_Ground01
</item>
<item>
019-Sa_Grass02
</item>
<item>
020-Sa_Grass03
</item>
<item>
021-Sa_Road01
</item>
<item>
022-Roof01
</item>
</autotile_names>
<panorama_name>
</panorama_name>
<panorama_hue>
0
</panorama_hue>
<fog_name>
</fog_name>
<fog_hue>
0
</fog_hue>
<fog_opacity>
64
</fog_opacity>
<fog_blend_type>
0
</fog_blend_type>
<fog_zoom>
200
</fog_zoom>
<fog_sx>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
the corruption of the original only gets worse from here on
to clarify I ran the import script and it generated the file tilesets.rmpy (at 18kb)
and the i ran the import to test the system and discovered this.
any idea what whent wrong? or if not how fix it, an alternative?
It seems that Gzip only works properly with files open in binary mode
require "rexml/document"
require "zlib"
include REXML
tilesetfile = File.new( "tilesets.rmpy", "wb" )
buffer = ""
tilesetgz = Zlib::GzipWriter.new(tilesetfile)
puts "Compressing output for: tilesets.rxdata ..."
tilesetdoc.write(buffer, 0)
tilesetgz.write(buffer)
tilesetgz.close
and
require "rexml/document"
require "zlib"
include REXML
tilesetfile = File.open("tilesets.rmpy", "rb")
tilesetgz = Zlib::GzipReader.new(tilesetfile)
tilesetdoc = Document.new tilesetgz.read.to_s
worked with out any problems

Resources