2

If I'm trying to post a question/answer on Stack Overflow, with some code that contains HTML text syntax inside, how do I do it nice with highlights. For example, I would like to post this line:

QString html = QString("<html><head><meta Content=\"Text/html; CHARSET=Windows-1251\"></head><body>%1</body></html>").arg(text);

But output is shown as QString html = QString("%1").arg(text);.

I have read this post what-html-tags-are-allowed-on-stack-overflow, but I didn't find the answer.

0

4 Answers 4

4

Select the text you want to highlight and click the 101 010 button:

int main() { return; }

alt text http://img210.imageshack.us/img210/2305/101010.png


QString html = QString("<html><head><meta Content=\"Text/html; CHARSET=Windows-1251\"></head><body>%1</body></html>").arg(text);
10
  • @Kop: thanks for reply, but that does not work with QString html = QString("<html><head><meta Content=\"Text/html; CHARSET=Windows-1251\"></head><body>%1</body></html>").arg(text); example.
    – mosg
    Commented Apr 28, 2010 at 11:22
  • @mosg: as you see from my edit it does work. Just select it and press the 101010 button. Commented Apr 28, 2010 at 11:33
  • @mosg sure it does. What are you talking about?
    – Pekka
    Commented Apr 28, 2010 at 11:33
  • @Kop & Pekka: I do posting as you sad, press "101 010" button on selected code line, and after that in preview section text looked like it cut all html syntax from the code line. Is this normal and it's all that I have is to press Post Question button?
    – mosg
    Commented Apr 28, 2010 at 11:40
  • +1 An unconventional (not red) freehand circle, but it definitely highlights the 101 010 button and gives meaning to the text.
    – tvanfosson
    Commented Apr 28, 2010 at 11:50
  • Ok, here is the full code example, which I'm trying to post with syntax highlights (as you can see, I have already post it, but with big troubles) - stackoverflow.com/questions/2727080/… There I have to use "101 010" with "Blockquote" button to gain such a representation of page witch is now...
    – mosg
    Commented Apr 28, 2010 at 12:16
  • 3
    @mosg - looks like there is a bug with the markdown preview (at least) when the code sample follows a list. Putting some text (or as you did a blockquote) between the two solves the issue with respect to the preview. I didn't check if the issue carried over to the live display. Since the mark up is produced server side for that it could certainly be ok even when the preview (which is implemented in javascript) isn't.
    – tvanfosson
    Commented Apr 28, 2010 at 12:25
  • @tvanfosson: I saw, that you correct my post, - thanks, so how did you do that?
    – mosg
    Commented Apr 28, 2010 at 13:07
  • @mosg - I have enough rep to be able to edit other's posts.
    – tvanfosson
    Commented Apr 28, 2010 at 19:13
  • @tvanfosson: Thanks for all replies. Just for now, I tested posting Qt & HTML code together, so it's looks good! :)
    – mosg
    Commented Apr 29, 2010 at 7:21
2

Besides formatting as code you can also URL encode "<" as "&lt;":

QString html = QString("&lt;html>&lt;head>&lt;meta Content=\"Text/html; CHARSET=Windows-1251\">&lt;/head>&lt;body>%1&lt;/body>&lt;/html>").arg(text);

It will display as: QString html = QString("<html><head><meta Content=\"Text/html; CHARSET=Windows-1251\"></head><body>%1</body></html>").arg(text);

3
  • @Peter Mortensen: just for now, I can't vote, but after gaining 15 reputations, I will vote for your reply! :) Thanks, you solution is one of best, but what if I need to post a few lines of such code? Do I have to replace all > symbols to &lt;?
    – mosg
    Commented Apr 28, 2010 at 12:21
  • @mosg: yes, replace all instances of "<" (it is not required for ">"). It is only required for HTML tags. Commented Apr 28, 2010 at 13:30
  • Hm, but some how, tvanfosson do the trick: in original post ( stackoverflow.com/questions/2727080/… ) he correct my post, without replacing "<" symbols... How did he do it - the secret for me...
    – mosg
    Commented Apr 28, 2010 at 14:05
2

Also, if you need your "code" to be inline -- as @Kop has done in naming the button -- you can use back ticks (``) to signify that the surrounded text is code. As in "what about the <html> tag?"

EDIT: Based on your comment, it appears to be a bug in the markdown preview when the code sample follows a list. All that is needed is some plain text between the list and the code sample.

0

Indent code by 4 spaces

QString html = QString("<html><head><meta Content=\"Text/html; CHARSET=Windows-1251\"></head><body>%1</body></html>").arg(text);

See https://meta.stackoverflow.com/editing-help

3
  • @Jeff Atwood: thank. I have read it before posting my question, but it seams that only I faced with this problem... strange...
    – mosg
    Commented Apr 28, 2010 at 11:36
  • 1
    @Jeff -- there appears to be a bug in the markdown preview when the code sample directly follows a list. See the link in the comments on @Kop's answer for the example. If you remove the "Sample Code" text (my edit), the preview doesn't show the code properly -- or at least didn't for me on my Mac using Safari.
    – tvanfosson
    Commented Apr 28, 2010 at 12:27
  • @tvan this old chestnut? meta.stackexchange.com/questions/19624/… Commented Apr 28, 2010 at 12:38

Not the answer you're looking for? Browse other questions tagged .