Introduction
Code Project (CP) is one of my favourite sites in the net. It is amazing how the team works under pressure speed solving problems or includes new features based on suggestions. But they are still humans, what means, they can't solve everything at once.
That's why some annoying things are still there, troubling the daily routine of the site.
Background
One source for tiny (or not so tiny) headaches is the article editor. There already are some very nice explanations about the editor and site formatting here in CP. Some I find good are:
But I keep seeing articles / tips / Q&A items having troubles with the code snippets.
For my explanation, I am using SQL code snippets extracted from Use SQL Server to Manage Completely DB Backups on Mirrored Databases. I have chosen this article, because it has had both the problems I am going to explain. If the article still is live, you can have a look to the versions history.
Issue 1: Copy and Paste Code from an Already Formatted Source
I would reckon that the author copied his code in his SQL environment (which is already formatted), when pasting here the colors of SQL environment were codified to HTML with the result...
<font color="#0000ff" face="Consolas" size="2">
<font color="#0000ff" face="Consolas" size="2">
<font color="#0000ff" face="Consolas" size="2">
EXEC</font></font></font>
How to insert code correctly is covered in OriginalGriff's tip in a professional way, but there is another possibility to avoid this using a simple workaround... paste it first in a pure plain text editor and then copy again. For Windows users, the best would be to use the Notepad.
In other words, copy in SQL and paste in Notepad, then copy in Notepad and paste in CP-Editor.
Doing this and choosing the correct language (Anshul's tip) in your code format (for this example "SQL") will give the result:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure'xp_cmdshell', 1
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell'
Issue 2: Escaped Quotes Mess the Format Coloring Everything as String
As we all know, sometimes when working with string / text, we have to escape some special symbols like [ \, ", ', : ] using another backslash before the special symbol [ \\, \", \', \: ]. This seems to drive the editor crazy and fool it with the result that it thinks everything afterwards is a string
and formats it using the color purple.
Getting back to the previous SQL article as an example...
iF @Folder_Path LIKE '%' + '\'
SET @Folder_Path = substring(@Folder_Path, 1, len(@Folder_Path) - len('\'))
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
CREATE TABLE #DirectoryTree (
id INT IDENTITY(1,1)
,fullpath VARCHAR(2000)
,subdirectory NVARCHAR(512)
,depth INT
,isfile BIT);
or using another format, like C#:
int Pos = file.LastIndexOf("\\");
if (Pos != null)
{
}
Path = Folder_Path + '\' + subdirectory
if (Path == null)
{
return;
}
// do other things
This can be simply solved by adding a comment with the same special symbol at the end of the line producing the chaos. Please note that the comment symbol must be appropriate for the programming language.
Using this workaround would give us the result:
iF @Folder_Path LIKE '%' + '\'
SET @Folder_Path = substring(@Folder_Path, 1, len(@Folder_Path) - len('\'))
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
CREATE TABLE #DirectoryTree (
id INT IDENTITY(1,1)
,fullpath VARCHAR(2000)
,subdirectory NVARCHAR(512)
,depth INT
,isfile BIT);
and...
int Pos = file.LastIndexOf("\\");
if (Pos != null)
{
}
Path = Folder_Path + '\' + subdirectory //'
if (Path == null)
{
return;
}
I know that this will become obsolete at some time because CP will have solved the bug, but in the meanwhile... I hope it helps you.
History
- 4th September, 2016 - v1.0 - Initial creation