Created
June 24, 2014 15:09
-
-
Save lnicola/0d97c756c69b0b2e5915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using System.Text; | |
| namespace TeXify | |
| { | |
| class Program | |
| { | |
| static bool inQuotes = false; | |
| static string FixLine(string line) | |
| { | |
| string result = ""; | |
| for (int i = 0; i < line.Length; i++) | |
| if (line[i] == '"') | |
| { | |
| if (inQuotes) | |
| result += "''"; | |
| else | |
| result += "``"; | |
| inQuotes = !inQuotes; | |
| } | |
| else | |
| result += line[i]; | |
| return result.Replace("...", "\\ldots "); //.Replace('ş', 'ș').Replace('ţ', 'ț').Replace('Ş', 'Ș').Replace('Ţ', 'Ț'); | |
| } | |
| static string ProcessFile(string fileName, StreamWriter fStreamWriter) | |
| { | |
| int start = fileName.LastIndexOf('\\') + 1, stop = fileName.LastIndexOf('.'); | |
| string title = fileName.Substring(start, stop - start); | |
| StreamReader streamReader = new StreamReader(File.OpenRead(fileName), Encoding.UTF8); | |
| string ptitle = streamReader.ReadLine().Replace("...", "\\ldots "); | |
| fStreamWriter.WriteLine("\\poemtitle{" + ptitle + "}"); | |
| fStreamWriter.WriteLine("\\begin{poem}"); | |
| string line, nextLine = streamReader.ReadLine(); | |
| while ((line = nextLine) != null) | |
| { | |
| nextLine = streamReader.ReadLine(); | |
| if (line != "") | |
| { | |
| line = FixLine(line); | |
| if (nextLine == "" || nextLine == null) | |
| line += "\n\\end{stanza}"; | |
| else | |
| if (!line.EndsWith("\\brokenline")) | |
| line += "\\verseline"; | |
| } | |
| else | |
| if (nextLine == "") | |
| line = "\\bigskip"; | |
| else | |
| line = "\\begin{stanza}"; | |
| fStreamWriter.WriteLine(line); | |
| } | |
| streamReader.Close(); | |
| fStreamWriter.WriteLine("\\end{poem}\n\\newpage"); | |
| return title; | |
| } | |
| static void Main(string[] args) | |
| { | |
| if (args.Length != 1) | |
| return; | |
| StreamWriter fstreamWriter = new StreamWriter(File.Create("poems.inc"), Encoding.UTF8); | |
| string path = args[0].Substring(0, args[0].LastIndexOf('\\') + 1); | |
| foreach (string fileName in File.ReadAllLines(args[0])) | |
| ProcessFile(path + fileName + ".txt", fstreamWriter); | |
| fstreamWriter.Close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment