-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·110 lines (102 loc) · 3.56 KB
/
Program.cs
File metadata and controls
executable file
·110 lines (102 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace adamant
{
class Program
{
const int ERROR_BAD_COMMAND = 22;
const ConsoleColor ErrColor = ConsoleColor.Red;
const ConsoleColor ErrColorDetail = ConsoleColor.DarkYellow;
public static void Main(string[] args)
{
if (args.Length == 1)
{
string result = ConvertFile(args[0]);
Console.WriteLine(result);
}
else
{
ConsoleColor startFg = Console.ForegroundColor;
Console.ForegroundColor = ErrColor;
Console.Error.Write("Error: ");
Console.ForegroundColor = ErrColorDetail;
Console.Error.WriteLine("Insert one argument only");
Console.ForegroundColor = startFg;
Environment.Exit(ERROR_BAD_COMMAND);
}
}
private static string ConvertFile(string fileName)
{
try
{
string text = File.ReadAllText(fileName);
// Ordering of this part is important!
text = AddParagraphTags(text);
text = AddFileTags(text);
return text;
}
catch (Exception ex)
{
ConsoleColor startFg = Console.ForegroundColor;
Console.ForegroundColor = ErrColor;
Console.Error.Write("Error: ");
Console.ForegroundColor = ErrColorDetail;
Console.Error.Write("Failed to convert " + fileName + ", reason: ");
Console.ForegroundColor = startFg;
Console.Error.WriteLine(ex);
}
return null;
}
private static string AddParagraphTags(string text)
{
var value = new StringBuilder();
List<string> paragraphs = ListParagraphs(text);
foreach (var paragraph in paragraphs)
{
value.AppendLine("<p>");
value.AppendLine(paragraph);
value.AppendLine("</p>");
}
return value.ToString();
}
private static string AddFileTags(string text)
{
var value = new StringBuilder();
value.AppendLine("<html>");
value.AppendLine("<body>");
value.AppendLine(text);
value.AppendLine("</body>");
value.AppendLine("</html>");
return value.ToString();
}
private static List<string> ListParagraphs(string text)
{
var textArray = new List<string>();
int pos = 0;
int prevPos = pos;
while (GetParagraphBreak(ref pos, text))
{
textArray.Add(text.Substring(prevPos, pos - prevPos));
pos += Environment.NewLine.Length * 2;
prevPos = pos;
}
textArray.Add(text.Substring(pos, text.Length - pos));
return textArray;
}
private static bool GetParagraphBreak(ref int pos, string text)
{
var searchFor = Environment.NewLine + Environment.NewLine;
var index = text.IndexOf(searchFor, pos);
if(index == -1)
{
return false;
}
pos = index;
return true;
}
}
}