Why is source code highlighting not working?
-
There is some documentation on “Posting Source Code” here: http://en.support.wordpress.com/code/posting-source-code/
But there seems to be something incomplete or wrong or typo’d in those instructions:
****
To accomplish the above, just wrap your code in these tags:your code here<br />****
That’s all it says,
tags. But wrapping code in` tags does NOT work to get syntax highlighting or preserve formatting. Something is missing. </p>
<p>I tried using the [sourcecode] shortcode’s from the open source WP plugin the wordpress.com feature is based on… and those seem to work, but I’m still having trouble getting them to preserve my code formatting, they keep doing odd things. I’d like an actual working example in docs to see if I’m doing things as supported or not. (Like if you really are meant to use the [sourcecode] shortcode, are you meant to insert<br />
<pre> tags too? The editor keeps sticking them in for me, although I’m not sure if they should be there or not, and I’m having trouble getting expected behavior either way.)
Blog url: http://bibwild.wordpress.com/</pre>
` -
Oh man, that accepted my code tags inline, heh. Try again:
To accomplish the above, just wrap your code in these tags:
your code here
****That’s all it says, code tags. But wrapping code in
tags does NOT work on my blog to get syntax highlighting or preserve formatting. (Although it seems to do so here in this forum!) Something is missing maybe. Or my blog isn’t working right?I tried using the [ ‘sourcecode’ ] shortcode’s from the open source WP plugin the wordpress.com feature is based on… and those seem to work, but I’m still having trouble getting them to preserve my code formatting, they keep doing odd things. I’d like an actual working example in docs to see if I’m doing things as supported or not. (Like if you really are meant to use the ‘sourcecode’ shortcode, are you meant to insert ‘pre’ tags too? The editor keeps sticking them in for me, although I’m not sure if they should be there or not, and I’m having trouble getting expected behavior either way.)
-
All you should have to do is wrap your code in the
<code>tag (with the appropriate closing tag, of course).Can you point me to a specific post where this isn’t working?
What language is it? If it’s an unrecognized language, WordPress.com will default back to text – no syntax highlighting.
-
It’s not currently on my blog because I could not get it to work, and didn’t want a non-working post on my blog.
Is there any way I can create a post that is not publically on my blog, but which you can still look at to debug? If so, I’ll do so. If not, maybe I’ll create a test post on my blog anyway if I need to.
I assume you are supposed to enter the ‘code’ tags in html source view, entering em in the visual editor would escape em and not do what you want, right?
And all the documented attributes are supposed to be supplied as attributes to the ‘code’ tag?
My problem is both that the tag doesn’t seem to do what it should, the attributes don’t seem to do what they should, AND the editor keeps corrupting what I entered in source view, changing the ‘code’ tags to ‘sourcecode’ shortcodes and/or adding ‘pre’ tags inside or outside them. The editor seems to do this if I switch from HTML mode to visual editor, but sometimes seems to do it even without a switch too.
I’ll create a test page demo’ing it, but would prefer if there were a way to do that that wasn’t on a publically published page? But if not, i’ll do it on a publicaly published page. Thanks.
-
I definitely recommend doing all of this in the HTML editor.
If possible, compose everything but the code excerpts in the Visual editor, then switch to the HTML editor to enter your code excerpts and publish or save the post without switching back to the Visual editor.
As for something that we can see, go ahead and save it as a Draft (without publishing it), and we can take a look from your Dashboard.
-
Okay, created the simplest possible test of basic behavior.
http://bibwild.wordpress.com/?p=1674&preview=true
Switched into HTML editing mode immediately, never switched back. Tried to put ‘code language=”ruby”‘ content on the post (in angle brackets as tags, just don’t know how to write that here without it being interpreted). The documentation at http://en.support.wordpress.com/code/posting-source-code/ lists ruby as a supported language.
Upon pressing ‘save’, the language=”ruby” attribute disappears. So it’s just ‘code’ tags, which has no syntax highlighting , which is probably expected when the language attribute is not on the tag?
To see if the problem is that ‘ruby’ is perhaps not really supported, let’s try a very popular language, php.
Added to the page ‘code language=”php”‘ tagged block…. wow, that time the behavior was even WORSE. Not only did it remove the ‘language=”php”‘ attribute when I hit ‘save’, so after ‘save’ it was a ‘code’ tag without a language attribute, and thus without syntax highligting — it also removed the entire contents of the block too. Perhaps because there were angle brackets contained in the content of the ‘code’ element — angle brackets are part of php code, right? And the support page claims “Code in between the source code tags will automatically be encoded for display, you don’t need to worry about HTML entities or anything.”
What I tried to put inside the ‘code’ block with language=”php” was:
left_angle-? echo ‘foo’; ?-right_angle
With left_angle and right_angle being actual angle brackets, but I don’t know how to write them here without them being interpreted/stripped.
-
I got some better luck with the old [sourcecode] short tag.
Check out the HTML view for an example, and give that a shot with your PHP code.
At some point during the testing I sent the post for review, but quickly changed it back to Draft. Sorry about that!
-
yes, i’m having better luck with ‘sourcecode’ short tag too, but it’s got some idiosyncracies too.
I guess I’m requesting that page of documentation be fixed to offer examples that really work — if that’s supposed to be ‘sourcecode’ short tag, that would be useful to have examples like that showing me intended supposed-to-work examples.
-
It really should work with the code tag, so I’ll have to look into this and see what we can do about that doc.
-
There are some additional idiosyncracies using the sourcecode short tag. I could report them in a different ticket, but it sounds like the main bug is that code tag isn’t working as documented, and if it were we should be using that rather than sourcecode shortcode, so it’s not really useful to report any additional issues until that is resolved?
-
We made just a few changes recently. Can you link us to the most recent example of the bug?
-
Re-did the test in the same draft post, exact same behavior as before, no change.
https://bibwild.wordpress.com/wp-admin/post.php?post=1674
It’s sort of a ‘bug’, but more like, contrary to your documentation, straight
<code>tags are NOT converted to sourcecode formatting, you need to use the[sourcecode]shorttag. The code tag is handled like any other tag, removing unrecognized attributes and html special characters needing to be escaped inside it.Are you sure you actually wrote code that was supposed to do this on
<code>tags like the documentation says? Maybe you forgot to to deploy it to production? -
Yes, it does look like that changed. We have updated the documentation to refer to only using the shortcode: http://en.support.wordpress.com/code/posting-source-code/
- The topic ‘Why is source code highlighting not working?’ is closed to new replies.