How to get title from HTTP text representation via Qt? - qt

Qt 4.7 DOM API seems kind of strange :(. I have a text HTML representation and need to get "title" text. Seems very easy, but following code is not working, i get empty string:
QDomDocument dom;
dom.setContent( "<html><head><title>this is a title</title></head></html>" );
QString title = dom.elementsByTagName( "title" ).item( 0 ).nodeValue();
Any suggestions?

Try this:
QString title = dom.elementsByTagName( "title" ).item( 0 ).firstChild().nodeValue();
Because the tree structure for text is:
node <title>
=> text element

Related

DocXtemplater multiple tags on a single line behavior

I am replacing an old program for template-merging with docxtemplater and am trying to recreate the old programs prefix functionality.
I want the line removed if all prefixed tags ({$tag}) on that line are undefined.
The issue being that if all the tags on that line are undefined docxtemplater still creates a blank line.
All the examples I have found online tend to reference inverted-sections or rawtags, which both seem to be designed for a single tag per line opposed to multiple tags side by side.
I have looked into using rawtags and writing a custom-parser / nullGetter. However I am still none the wiser to removing the blank line.
I am using:
const options = {
paragraphLoop: true,
linebreaks: false,
parser: function(tag) {
return {
get(scope, context) {
console.log(tag);
console.log(scope);
console.log(context);
if (tag[0] == "$") {
tag = tag.substr(1); // needs to then remove line break
}
return scope[tag];
}
}
},
nullGetter: function nullGetter(part, scopeManager) {
if (!part.module) {
return "";
}
if (part.module === "rawxml") {
return "";
}
return "";
}
};
doc = new Docxtemplater(zip, options);
The prefix in the program I am replacing acts as follows:
data:
existingtag: EXISTINGTAG
Template.docx:
1 text above
{$existingtag}{$nonexistingtag}
text below
2 text above
{$existingtag}{$existingtag}
text below
3 text above
{$nonexistingtag}{$nonexistingtag}
text below
old program produced (What I want to produce)
1 text above
EXISTINGTAG
text below
2 text above
EXISTINGTAGEXISTINGTAG
text below
3 text above
text below
my docxtemplater produces (extra line in example 3):
1 text above
EXISTINGTAG
text below
2 text above
EXISTINGTAGEXISTINGTAG
text below
3 text above
text below
I'm the creator of docxtemplater and I don't think that there is a way to do what you want to achieve without taking a lot of time to handle this case.
The problem is that the tags such as :
{xxx}{yyy}
have access only to the text that they are in, but they cannot have any effect ouside of that text, so it is not possible to remove a paragraph conditionnally.
There is one tag that has access to the whole paragraph, that is the raw xml tag, prefixed by a "#", like this :
{#raw}
It is used to add rawXML and if the raw value is an empty string, than that paragraph will be removed.
Edit : I have actually worked on a module back in the time to achieve quite similar functionnality, it is a paid module : https://docxtemplater.com/modules/paragraph-placeholder/

How to display plain text using QMessageBox?

I need to display an HTML text:
QString text="<b>Hello</b>";
QMessageBox::information(this,"info", text);
The text is displayed as bold "Hello". How to display it as it is, i.e.,
<b>Hello</b>
Thanks!
You have to use the method toHtmlEscaped() of QString:
QString text="<b>Hello</b>";
QMessageBox::information(this, "info", text.toHtmlEscaped());

How to send an Embedded Image along with text in a Message via Telegram Bot API

Using Telegram Bot API,
I'm aware that it is possible to send an image via https://core.telegram.org/bots/api#sendphoto
However, how can I embed a remote image into a formatted message?
The message I am looking to send, can be compared to a news article with a title in bold, an image, and a longer text with links. I figured out how to create bold text and links with markdown, but I'm failing at inserting images. How can we do that?
you must set ParseMode in HTML and set your Image Url in A tag like this:
‍
‍ -> never show in message
You can use zero-width space trick. Works for both Markdown and HTML parse mode.
Markdown:
$data = [
'chat_id' => $chat_id,
'parse_mode' => 'markdown',
'text' => "[​​​​​​​​​​​](https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/200px-Stack_Overflow_logo.svg.png) Some text here.",
];
Result:
Note: The zero-width space is in-between the brackets "[​​​​​​​​​​​]".
import requests
text="testing"
img="http://imageurl.png"
r = requests.get('https://api.telegram.org/botyour_token_here/sendMessage?chat_id=#your_channel_here&parse_mode=markdown&text='+"[​​​​​​​​​​​]("+img+")"+text)
Method using <a href=http://.......jpg>..</a> will show preview of the image below the text.
Like this:
a href sample
It will look better if you send an image with a caption.
caption sample
You should just add captions
bot.send_video(user_id, video, caption='some interesting text')
In our case captions are text. look this image
Using sendPhoto rather than sendMessage is a cleaner way of achieving this, depending on your use case, for example:
import io
import json
import requests
telegram_bot_token = 'INSERT_TOKEN_HERE'
chat_id = '#INSERT_CHAT_ID_HERE'
bot_url = 'https://api.telegram.org/bot' + telegram_bot_token + '/sendPhoto'
img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/200px-Stack_Overflow_logo.svg.png'
msg_txt = '<b>Stack Overflow Logo</b>'
msg_txt += '\n\nStack Overflow solves all our problems'
payload = {
'chat_id': chat_id,
'caption': msg_txt,
'parse_mode': 'html'
}
remote_image = requests.get(img_url)
photo = io.BytesIO(remote_image.content)
photo.name = 'img.png'
files = {'photo': photo}
req = requests.post(url=bot_url, data=payload, files=files)
response = req.json()
print(response)

Accessing text between two QWebElement objects

I am traversing a DOM using Qt's WebKit classes. Please have a look on the following pseudo HTML:
<br>111<a class="node">AAA</a>
<br>222<a class="node">BBB</a>
...
I can easily find the anchors using findAll(). However I also need to get the text before the elements ("111" and "222"). I tried to use previousSibling() but of course that gives me the <br> element since the "111" and "222" texts are no elements.
I found a function to access text within an element, but how can I access between the <br> and the <a> elements?
It seems it is not possible. The only workaround I could find is getting the plain text of the parent node and parsing the resulting plain text.
This is the way I solved it:
QWebElement *element = ...
// find out if QWebElement has text
QDomDocument doc;
doc.setContent(element->toOuterXml());
QDomElement domelem = doc.documentElement();
for(QDomNode n = domelem.firstChild(); !n.isNull(); n = n.nextSibling())
{
QDomText t = n.toText();
if (!t.isNull())
{
// it has text !
qDebug() << t.data();
break;
}
}

Convert HTML Email into plain text, is it possible to add colour / style

We currently have a bad situation where I need to change HTML Body content from a CMS and convert it to plain text so it can go into the body of an emailto.
private static string StripHtmlToAscii(string mergeEmailBody)
{
var asciiEmailBody = mergeEmailBody;
asciiEmailBody = asciiEmailBody.Replace("<br />", "%0D%0A").Replace("<br/>", "%0D%0A").Replace("+", "%20").Replace(" ", "%20").Replace("<p>", "").Replace("</p>", "%0D%0A%0D%0A").Replace("<br>", "%0D%0A");
return asciiEmailBody;
}
See above..
I am wondering is it possible to add style like bold or colour to a plain text email ?
No, plain text is plain text, it will only accept text. The only way you can add colour is by sending an HTML email

Resources