{"id":2647,"date":"2008-07-15T02:45:00","date_gmt":"2008-07-15T02:45:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/pasting-code-into-a-blog\/"},"modified":"2016-07-28T10:49:19","modified_gmt":"2016-07-28T10:49:19","slug":"pasting-code-into-a-blog","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/pasting-code-into-a-blog\/","title":{"rendered":"Pasting code into a Blog."},"content":{"rendered":"<p>One reason that I often hear for the apparent inability of certain people to blog is that there is no way for Community Server to display code. It isn&#8217;t actually that hard. The obvious choice is to use <a href=\"http:\/\/extras.sqlservercentral.com\/prettifier\/prettifier.aspx?skin=st\">Phil Factor&#8217;s prettifier<\/a>. It is the reason he wrote it. If you need to show exactly what is in Visual Studio, or SSMS, there is another way. <\/p>\n<p>The problem you have is that Community Server is usually set to only allow FONT tags to enhance text. This makes life difficult, though it ensures that a blogger can&#8217;t cause too much havoc with surrounding parts of the page.<\/p>\n<p>Paste your code into Word. Make sure that it has pasted it in, using the &#8216;keep source formatting&#8217; option. Then, save it as HTML. Alternatively, you can use Outlook. (I set the format of the message to be HTML. I then&#160;send the code to myself, right-click the results and select &#8216;view source code&#8217;.)<\/p>\n<p>I then take&#160;the HTML code&#160;into a nice text editor, &#8216;top and tail&#8217; it (take out the HTML, BODY tags and the DIVs, leaving just the Ps in place) &#160;and do the following substitutions with the objective of taking out the SPANs (no need really as they don&#8217;t get rendered anyway) and changing the Ps into BRs<\/p>\n<table>\n<tbody>\n<tr>\n<th>FROM<\/th>\n<th>TO<\/th>\n<th>Result<\/th>\n<\/tr>\n<tr>\n<td>style=&#8217;font-size:11.0pt;font-family:&#8221;Courier New&#8221;&#8216;<\/td>\n<td><\/td>\n<td>Deleted all occurences<\/td>\n<\/tr>\n<tr>\n<td>&lt;\/p&gt;rn&lt;p class=MsoNormal<\/td>\n<td>&lt;BR<\/td>\n<td>(this is RegEx) correct the spacing between lines. replace all paragraph tags with line break tabs<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You may find that the font has been styled for a different size, depending on the setting in Visual Studio. This means that yo will need to remove style=&#8217;font-size:10.0pt;font-family:&#8221;Courier New&#8221;&#8216; rather than 11.0pt. <\/p>\n<p>Then create, or edit, the blog. Paste the&#160;modified code into the HTML pane (the tab is on the bottom-right of the panel in Community server) Select the code and, in the design pane, make the size of the code 2 rather than 3. Select the code and specify that it should be Courier<\/p>\n<p>All that we&#8217;re actually doing here is trying to remove all the <strong>Word<\/strong> silliness of trying to be all things to all men. All you actually need is the colour information and the non-breaking spaces to do the indentation. The final substitution makes the paragraph tag render as you want it to.<\/p>\n<hr \/>\n<p>ALTER Function [dbo].[ufsRemoveDelimited]<br \/>&#160;&#160;&#160; (<br \/>&#160;&#160;&#160;&#160;&#160; @String VARCHAR(MAX),<br \/>&#160;&#160;&#160;&#160;&#160; @OpeningDelimiter CHAR(1),<br \/>&#160;&#160;&#160;&#160;&#160; @ClosingDelimiter CHAR(1)<br \/>&#160;&#160;&#160; )<br \/>RETURNS VARCHAR(8000)<br \/>AS BEGIN <br \/>&#160;&#160;&#160; DECLARE @newString VARCHAR(8000) <br \/>&#160;&#160;&#160; IF @OpeningDelimiter = @ClosingDelimiter <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; BEGIN <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RETURN NULL <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END <br \/>&#160;&#160;&#160; IF @OpeningDelimiter + @ClosingDelimiter + @String IS NULL <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; BEGIN <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RETURN @String <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END <br \/>&#160;&#160;&#160; SELECT&#160; @NewString = &#8221; <br \/>&#160;&#160;&#160; SELECT&#160; @newString = @newString + SUBSTRING(@String, number, 1)<br \/>&#160;&#160;&#160; FROM&#160;&#160;&#160; numbers<br \/>&#160;&#160;&#160; WHERE&#160;&#160; number &lt;= LEN(REPLACE(@string, &#8216; &#8216;, &#8216;|&#8217;))<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND CHARINDEX(@OpeningDelimiter, @string + @OpeningDelimiter,<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; number) &lt; CHARINDEX(@ClosingDelimiter,<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @string + &#8216; &#8216;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + @closingDelimiter, number)<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND number &lt;&gt; CHARINDEX(@OpeningDelimiter, @string, number) <br \/>&#160;&#160;&#160; RETURN @NewString <br \/>&#160;&#160; END<\/p>\n<hr \/>\n<p>Here is some C#, copied out of Visual Studio 2005<\/p>\n<hr \/>\n<p>\/\/ Marshal.cs<br \/>using System;<br \/>using System.Runtime.InteropServices;<br \/>&#160;<br \/>class PlatformInvokeTest<br \/>{<br \/>&#160;&#160;&#160; [DllImport(&#8220;msvcrt.dll&#8221;)]<br \/>&#160;&#160;&#160; public static extern int puts(<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [MarshalAs(UnmanagedType.LPStr)]<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; string m);<br \/>&#160;&#160;&#160; [DllImport(&#8220;msvcrt.dll&#8221;)]<br \/>&#160;&#160;&#160; internal static extern int _flushall();<br \/>&#160;<br \/>&#160;<br \/>&#160;&#160;&#160; public static void Main()<br \/>&#160;&#160;&#160; {<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; puts(&#8220;Hello World!&#8221;);<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _flushall();<br \/>&#160;&#160;&#160; }<br \/>}<\/p>\n<hr \/>\n<p>Here is a small sample of VB code<\/p>\n<hr \/>\n<p>Private Sub Button1_Click(ByVal sender As System.Object, _<br \/>&#160;&#160;&#160; ByVal e As System.EventArgs) Handles Button1.Click<br \/>&#160;<br \/>&#160;&#160;&#160; &#8216; Stores the return value.<br \/>&#160;&#160;&#160; Dim RetVal As Integer<br \/>&#160;&#160;&#160; RetVal = MBox(0, &#8220;Declare DLL Test&#8221;, &#8220;Windows API MessageBox&#8221;, _<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; MB_ICONQUESTION Or MB_YESNO)<br \/>&#160;<br \/>&#160;&#160;&#160; &#8216; Check the return value.<br \/>&#160;&#160;&#160; If RetVal = IDYES Then<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; MsgBox(&#8220;You chose Yes&#8221;)<br \/>&#160;&#160;&#160; Else<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; MsgBox(&#8220;You chose No&#8221;)<br \/>&#160;&#160;&#160; End If<br \/>End Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One reason that I often hear for the apparent inability of certain people to blog is that there is no way for Community Server to display code. It isn&#8217;t actually that hard. The obvious choice is to use Phil Factor&#8217;s prettifier. It is the reason he wrote it. If you need to show exactly what&#8230;&hellip;<\/p>\n","protected":false},"author":9580,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2647","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/9580"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2647"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2647\/revisions"}],"predecessor-version":[{"id":41641,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2647\/revisions\/41641"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2647"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}