{"id":1229,"date":"2011-10-04T03:50:28","date_gmt":"2011-10-04T08:50:28","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?p=1229"},"modified":"2011-10-04T03:56:07","modified_gmt":"2011-10-04T08:56:07","slug":"how-to-add-syntax-colored-and-well-formatted-sql-in-your-wordpress-blog","status":"publish","type":"post","link":"https:\/\/www.dpriver.com\/blog\/2011\/10\/how-to-add-syntax-colored-and-well-formatted-sql-in-your-wordpress-blog\/","title":{"rendered":"How to add syntax colored and well formatted SQL in your wordpress blog"},"content":{"rendered":"

People always post SQL code into blogs and forums need a tool to help them to prepare dull monochrome, badly-formatted SQL code for HTML-based publication in a nice clean style. <\/p>\n

SQL Pretty Printer<\/a> is able to output HTML in both <code> and <pre> blocks in a convenient way with code like this:<\/p>\n

    \r\n\t<div><span style=\"font-family: Courier New; font-size: 10pt;\">\r\n\t<span style=\"color: blue; \">WITH<\/span> <span style=\"color: maroon; \">mycte<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: maroon; \">x<\/span><span style=\"color: maroon; \">)<\/span>\r\n\t<br\/>     <span style=\"color: blue; \">AS<\/span> <span style=\"color: maroon; \">(<\/span><span style=\"color: blue; \">SELECT<\/span> <span style=\"color: maroon; \">x<\/span> <span style=\"color: silver; \">=<\/span> <span style=\"color: fuchsia; font-style: italic; \">CONVERT<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: black; font-style: italic; \">VARCHAR<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: black; \">1000<\/span><span style=\"color: maroon; \">)<\/span><span style=\"color: silver; \">,<\/span> <span style=\"color: red; \">'hello'<\/span><span style=\"color: maroon; \">)<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">UNION<\/span> <span style=\"color: blue; \">ALL<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">SELECT<\/span> <span style=\"color: fuchsia; font-style: italic; \">CONVERT<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: black; font-style: italic; \">VARCHAR<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: black; \">1000<\/span><span style=\"color: maroon; \">)<\/span><span style=\"color: silver; \">,<\/span> <span style=\"color: maroon; \">x<\/span> <span style=\"color: silver; \">+<\/span> <span style=\"color: red; \">'a'<\/span><span style=\"color: maroon; \">)<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">FROM<\/span>   <span style=\"color: maroon; \">mycte<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">WHERE<\/span>  <span style=\"color: fuchsia; font-style: italic; \">Len<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: maroon; \">x<\/span><span style=\"color: maroon; \">)<\/span> <span style=\"color: silver; \"><<\/span> <span style=\"color: black; \">10<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">UNION<\/span> <span style=\"color: blue; \">ALL<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">SELECT<\/span> <span style=\"color: fuchsia; font-style: italic; \">CONVERT<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: black; font-style: italic; \">VARCHAR<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: black; \">1000<\/span><span style=\"color: maroon; \">)<\/span><span style=\"color: silver; \">,<\/span> <span style=\"color: maroon; \">x<\/span> <span style=\"color: silver; \">+<\/span> <span style=\"color: red; \">'b'<\/span><span style=\"color: maroon; \">)<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">FROM<\/span>   <span style=\"color: maroon; \">mycte<\/span>\r\n\t<br\/>         <span style=\"color: blue; \">WHERE<\/span>  <span style=\"color: fuchsia; font-style: italic; \">Len<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: maroon; \">x<\/span><span style=\"color: maroon; \">)<\/span> <span style=\"color: silver; \"><<\/span> <span style=\"color: black; \">10<\/span><span style=\"color: maroon; \">)<\/span>\r\n\t<br\/><span style=\"color: blue; \">SELECT<\/span> <span style=\"color: maroon; \">x<\/span>\r\n\t<br\/><span style=\"color: blue; \">FROM<\/span>   <span style=\"color: maroon; \">mycte<\/span>\r\n\t<br\/><span style=\"color: blue; \">ORDER<\/span>  <span style=\"color: blue; \">BY<\/span> <span style=\"color: fuchsia; font-style: italic; \">Len<\/span><span style=\"color: maroon; \">(<\/span><span style=\"color: maroon; \">x<\/span><span style=\"color: maroon; \">)<\/span><span style=\"color: silver; \">,<\/span>\r\n\t<br\/>          <span style=\"color: maroon; \">x<\/span><span style=\"color: silver; \">;<\/span> \r\n\t<\/span><\/div>\r\n<\/pre>\nYou can copy and embedded above HTML code into your blog to get a colored SQL like this:<\/p>\n
    \nWITH<\/span> mycte<\/span>(<\/span>x<\/span>)<\/span>
    \n
         AS<\/span> (<\/span>SELECT<\/span> x<\/span> =<\/span> CONVERT<\/span>(<\/span>VARCHAR<\/span>(<\/span>1000<\/span>)<\/span>,<\/span> ‘hello’<\/span>)<\/span>
    \n
             UNION<\/span> ALL<\/span>
    \n
             SELECT<\/span> CONVERT<\/span>(<\/span>VARCHAR<\/span>(<\/span>1000<\/span>)<\/span>,<\/span> x<\/span> +<\/span> ‘a’<\/span>)<\/span>
    \n
             FROM<\/span>   mycte<\/span>
    \n
             WHERE<\/span>  Len<\/span>(<\/span>x<\/span>)<\/span> <<\/span> 10<\/span>
    \n
             UNION<\/span> ALL<\/span>
    \n
             SELECT<\/span> CONVERT<\/span>(<\/span>VARCHAR<\/span>(<\/span>1000<\/span>)<\/span>,<\/span> x<\/span> +<\/span> ‘b’<\/span>)<\/span>
    \n
             FROM<\/span>   mycte<\/span>
    \n
             WHERE<\/span>  Len<\/span>(<\/span>x<\/span>)<\/span> <<\/span> 10<\/span>)<\/span>
    \n
    SELECT<\/span> x<\/span>
    \n
    FROM<\/span>   mycte<\/span>
    \n
    ORDER<\/span>  BY<\/span> Len<\/span>(<\/span>x<\/span>)<\/span>,<\/span>
    \n
              x<\/span>;<\/span>
    \n<\/span><\/div>\nBut in wordpress, the output will be looked like this that add additional lines.<\/p>\n
    \nWITH<\/span> mycte<\/span>(<\/span>x<\/span>)<\/span><\/p>\n     AS<\/span> (<\/span>SELECT<\/span> x<\/span> =<\/span> CONVERT<\/span>(<\/span>VARCHAR<\/span>(<\/span>1000<\/span>)<\/span>,<\/span> ‘hello’<\/span>)<\/span><\/p>\n         UNION<\/span> ALL<\/span><\/p>\n         SELECT<\/span> CONVERT<\/span>(<\/span>VARCHAR<\/span>(<\/span>1000<\/span>)<\/span>,<\/span> x<\/span> +<\/span> ‘a’<\/span>)<\/span><\/p>\n         FROM<\/span>   mycte<\/span><\/p>\n         WHERE<\/span>  Len<\/span>(<\/span>x<\/span>)<\/span> <<\/span> 10<\/span><\/p>\n         UNION<\/span> ALL<\/span><\/p>\n         SELECT<\/span> CONVERT<\/span>(<\/span>VARCHAR<\/span>(<\/span>1000<\/span>)<\/span>,<\/span> x<\/span> +<\/span> ‘b’<\/span>)<\/span><\/p>\n         FROM<\/span>   mycte<\/span><\/p>\n         WHERE<\/span>  Len<\/span>(<\/span>x<\/span>)<\/span> <<\/span> 10<\/span>)<\/span><\/p>\nSELECT<\/span> x<\/span><\/p>\nFROM<\/span>   mycte<\/span><\/p>\nORDER<\/span>  BY<\/span> Len<\/span>(<\/span>x<\/span>)<\/span>,<\/span><\/p>\n          x<\/span>;<\/span>
    \n<\/span><\/div>\nIt seems that editor of wordpress add an additional <br\/> tag at the end of each line. this issue can be resolved by changing<\/p>\n\r\nfunction wpautop($pee, $br = 1) {\r\n<\/pre>\nto<\/p>\n\r\nfunction wpautop($pee, $br = 0) {\r\n<\/pre>\nin wp-includes\/formatting.php<\/p>\n","protected":false},"excerpt":{"rendered":"People always post SQL code into blogs and forums need a tool to help them to prepare dull monochrome, badly-formatted SQL code for HTML-based publication in a nice clean style. SQL Pretty Printer is able to output HTML in both <code> and <pre> blocks in a convenient way with code like this: <div><span style=”font-family: Courier New; font-size: 10pt;”> <span style=”color: blue; “>WITH<\/span> <span style=”color: maroon; “>mycte<\/span><span style=”color: maroon; “>(<\/span><span style=”color: maroon; “>x<\/span><span style=”color: maroon; “>)<\/span> <br\/>     <span style=”color: blue; “>AS<\/span> <span style=”color: maroon; “>(<\/span><span style=”color: blue; “>SELECT<\/span> <span style=”color: maroon; “>x<\/span> <span style=”color: silver; “>=<\/span> <span style=”color: fuchsia; font-style: italic; “>CONVERT<\/span><span style=”color: maroon; “>(<\/span><span style=”color: black;\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,25,21],"tags":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"\nHow to add syntax colored and well formatted SQL in your wordpress blog<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n