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
I come from Spain. After making a lot of silly things during the studies, I wanted to correct me and to make something positive with my life, so I asked for (and got) an “Erasmus” scholarship to go to Germany. After obtaining a placement to make my Thesis in a Firm of Automation and Software development, I reached the double Degree in Electronics’ Engineering and Informatic. I worked a time with VC++ 6, many years with industry PLC and robot programming, done backbone IT for something related to car electronics and production, back to my roots in C++ and PLC Programming again and currently working as system architect for my division.
I contrinue trying to learn more C# in my spare time (which is even less than before, God bless my kids)