{"id":449,"date":"2011-06-28T21:53:52","date_gmt":"2011-06-28T19:53:52","guid":{"rendered":"http:\/\/visurus.wordpress.com\/?p=449"},"modified":"2011-06-28T21:53:52","modified_gmt":"2011-06-28T19:53:52","slug":"syntax-highlighting-on-wordpress-com","status":"publish","type":"post","link":"https:\/\/www.ralphstraumann.ch\/blog\/2011\/06\/syntax-highlighting-on-wordpress-com\/","title":{"rendered":"Syntax highlighting on WordPress.com"},"content":{"rendered":"<p>In order to present readable and usable code here on WordPress.com without manually formatting it I searched the intertubes for an elegant solution\u00a0\u2013\u00a0and found one <a href=\"http:\/\/en.support.wordpress.com\/code\/posting-source-code\/\">here<\/a>.<\/p>\n<p>[sourcecode language=&#8221;python&#8221; gutter=&#8221;false&#8221;]<br \/>\nimport math, string<\/p>\n<p>def readFile(file):<br \/>\n    &quot;&quot;&quot;Reads a file from disk and returns its content as a string variable<br \/>\n    :param file: path to a file to be read<br \/>\n    &quot;&quot;&quot;<br \/>\n    with open(file, &#8216;r&#8217;) as f:<br \/>\n        data = f.read()<br \/>\n    return data<\/p>\n<p>def countCharsWhitespace(data):<br \/>\n    &quot;&quot;&quot;Returns the number of characters in a string as an integer,<br \/>\n       including whitespace characters and punctuation marks, but<br \/>\n       not newline characters<br \/>\n    :param data: string variable containing the text to be analysed<br \/>\n    &quot;&quot;&quot;<br \/>\n    data = data.replace(&#8216;n&#8217;,&#8221;)<br \/>\n    return len(data)<\/p>\n<p>def countWords(data):<br \/>\n    &quot;&quot;&quot;Returns the number of words in a string as an integer<br \/>\n    :param data: string variable containing the text to be analysed<br \/>\n    &quot;&quot;&quot;<br \/>\n    return len(data.split())[\/sourcecode]<\/p>\n<p>The syntax highlighting works with what WordPress seems to call a shortcode: <em>[ sourcecode ]<\/em>. Presently <a href=\"http:\/\/en.support.wordpress.com\/code\/posting-source-code\/\">some 25 languages are supported<\/a>\u00a0and various options further customise the look and feel of the code representation (for example, line numbers could easily be added to above display):<\/p>\n<ul>\n<li>autolinks (true\/false) \u2014 Makes all URLs in your posted code clickable. Defaults to true.<\/li>\n<li>collapse (true\/false) \u2014 If true, the code box will be collapsed when the page loads, requiring the visitor to click to expand it. Good for large code posts. Defaults to false.<\/li>\n<li>firstline (number) \u2014 Use this to change what number the line numbering starts at. It defaults to 1.<\/li>\n<li>gutter (true\/false) \u2014 If false, the line numbering on the left side will be hidden. Defaults to true.<\/li>\n<li>highlight (comma-seperated list of numbers) \u2014 You can list the line numbers you want to be highlighted. For example \u201c4,7,19\u2033.<\/li>\n<li>htmlscript (true\/false) \u2014 If true, any HTML\/XML in your code will be highlighted. This is useful when you are mixing code into HTML, such as PHP inside of HTML. Defaults to false and will only work with certain code languages.<\/li>\n<li>light (true\/false) \u2014 If true, the gutter (line numbering) and toolbar (see below) will be hidden. This is helpful when posting only one or two lines of code. Defaults to false.<\/li>\n<li>padlinenumbers (true\/false\/integer) \u2014 Allows you to control the line number padding. true will result in automatic padding, false will result in no padding, and entering a number will force a specific amount of padding.<\/li>\n<li>toolbar (true\/false) \u2014 If false, the toolbar containing the helpful buttons that appears when you hover over the code will not be shown. Defaults to true.<\/li>\n<li>wraplines (true\/false) \u2014 If true, line line wrapping will be disabled. This will cause a horizontal scrollbar to appear for long lines of code.<\/li>\n<\/ul>\n<p>(and yes, for one of my next projects I&#8217;m looking into (very basic!) language analysis ;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to present readable and usable code here on WordPress.com without manually formatting it I searched the intertubes for an elegant solution\u00a0\u2013\u00a0and found one here. [sourcecode language=&#8221;python&#8221; gutter=&#8221;false&#8221;] import math, string def readFile(file): &quot;&quot;&quot;Reads a file from disk and returns its content as a string variable :param file: path to a file to be &hellip; <a href=\"https:\/\/www.ralphstraumann.ch\/blog\/2011\/06\/syntax-highlighting-on-wordpress-com\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Syntax highlighting on WordPress.com<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[66,67,191,92,104,113,124],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-note","tag-java","tag-javascript","tag-meta","tag-python","tag-source-code","tag-text-analysis","tag-visual"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3pPwF-7f","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":0,"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ralphstraumann.ch\/blog\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}