public USFMDocument ParseFromString(string input) { Regex splitRegex = new Regex("\\\\([a-z0-9*]+)([^\\\\]*)"); USFMDocument output = new USFMDocument(); foreach (Match match in splitRegex.Matches(input)) { if (IgnoredTags.Contains(match.Groups[1].Value)) { continue; } ConvertToMarkerResult result = ConvertToMarker(match.Groups[1].Value, match.Groups[2].Value); if (result.marker is TRMarker && !output.GetTypesPathToLastMarker().Contains(typeof(TableBlock))) { output.Insert(new TableBlock()); } output.Insert(result.marker); if (!string.IsNullOrWhiteSpace(result.remainingText)) { output.Insert(new TextBlock(result.remainingText)); } } return(output); }
private void RenderHtml(string fileName) { // Does not parse through section headers yet var parser = new USFMParser(new List <string> { "s5" }); HTMLConfig configHTML = BuildHTMLConfig(); //Configure Settings -- Spacing ? 1, Column# ? 1, TextDirection ? L2R var renderer = new HtmlRenderer(configHTML); // Added ULB License and Page Number renderer.FrontMatterHTML = GetLicenseInfo(); renderer.InsertedFooter = GetFooterInfo(); var usfm = new USFMDocument(); var progress = fileDataGrid.RowCount - 1; var progressStep = 0; foreach (DataGridViewRow row in fileDataGrid.Rows) { var cell = row.Cells[0]; if (cell.Value == null) { continue; } var filename = cell.Value.ToString(); var text = File.ReadAllText(filename); usfm.Insert(parser.ParseFromString(text)); progressStep++; LoadingBar.Value = (int)(progressStep / (float)progress * 100); } var html = renderer.Render(usfm); File.WriteAllText(fileName, html); var dirname = Path.GetDirectoryName(fileName); filePathConversion = dirname; var cssFilename = Path.Combine(dirname, "style.css"); if (!File.Exists(cssFilename)) { File.Copy("style.css", cssFilename); } }
/// <summary> /// Parses a string into a USFMDocument /// </summary> /// <param name="input">A USFM string</param> /// <returns>A USFMDocument representing the input</returns> public USFMDocument ParseFromString(string input) { USFMDocument output = new USFMDocument(); var markers = TokenizeFromString(input); foreach (Marker marker in markers) { if (marker is TRMarker && !output.GetTypesPathToLastMarker().Contains(typeof(TableBlock))) { output.Insert(new TableBlock()); } var markerIndex = markers.IndexOf(marker); if (marker is QMarker && markerIndex != markers.Count - 1 && markers[markerIndex + 1] is VMarker) { ((QMarker)marker).IsPoetryBlock = true; } output.Insert(marker); } return(output); }
private async void BtnCheck_Click(object sender, EventArgs e) { Msg_Linter.ResetText(); BtnCheck.Enabled = false; BtnAddFiles.Enabled = false; var parser = new USFMParser(new List <string> { "s5" }); var usfm = new USFMDocument(); DataGridViewSelectedCellCollection SelectedFiles = fileDataGrid.SelectedCells; USFMLinter lint = new USFMLinter(); if (fileDataGrid.Rows.Count > 1) { foreach (DataGridViewCell SelectFile in SelectedFiles) { if (SelectFile.OwningRow.Index != fileDataGrid.RowCount - 1) { var cell = SelectFile.OwningRow.Cells[0]; if (cell.Value == null) { continue; } var filename = cell.Value.ToString(); var text = File.ReadAllText(filename); usfm.Insert(parser.ParseFromString(text)); List <LinterResult> linterResults = await lint.LintAsync(usfm); await DisplayErrors(linterResults, filename); usfm = new USFMDocument(); } } } BtnCheck.Enabled = true; BtnAddFiles.Enabled = true; }
private void RenderDocx(string fileName) { // Does not parse through section headers yet var parser = new USFMParser(new List <string> { "s5" }); var renderer = new DocxRenderer(BuildDocxConfig()); var usfm = new USFMDocument(); var progress = fileDataGrid.RowCount - 1; var progressStep = 0; foreach (DataGridViewRow row in fileDataGrid.Rows) { var cell = row.Cells[0]; if (cell.Value == null) { continue; } var filename = cell.Value.ToString(); var text = File.ReadAllText(filename); usfm.Insert(parser.ParseFromString(text)); progressStep++; LoadingBar.Value = (int)(progressStep / (float)progress * 100); } var output = renderer.Render(usfm); using (Stream outputStream = File.Create(fileName)) { output.Write(outputStream); } }
public void Run() { // Does not parse through section headers yet var parser = new USFMParser(new List <string> { "s5" }); configHTML = BuildConfig(); //Configure Settings -- Spacing ? 1, Column# ? 1, TextDirection ? L2R var renderer = new HtmlRenderer(configHTML); // Added ULB License and Page Number renderer.FrontMatterHTML = GetLicenseInfo(); renderer.InsertedFooter = GetFooterInfo(); var usfm = new USFMDocument(); if (folderName.Equals(string.Empty)) { return; } var dirinfo = new DirectoryInfo(folderName); if (!dirinfo.Exists) { Console.WriteLine("The input path doesn't exist. Make sure you spelled it correctly"); return; } if (!Directory.Exists(projectOutput)) { Console.WriteLine("The output path doesn't exist. Make sure you spelled it correctly"); return; } var allFiles = dirinfo.GetFiles("*.usfm", SearchOption.AllDirectories) .Union(dirinfo.GetFiles("*.txt", SearchOption.AllDirectories)).ToArray(); Array.Sort(allFiles, delegate(FileInfo a, FileInfo b) { return(a.FullName.CompareTo(b.FullName)); }); var progress = allFiles.Length; var progressStep = 0; foreach (FileInfo fileInfo in allFiles) { var text = File.ReadAllText(fileInfo.FullName); usfm.Insert(parser.ParseFromString(text)); progressStep++; Console.Write("\r[{0}%] ", (int)(progressStep / (float)progress * 100)); } var html = renderer.Render(usfm); var htmlFilename = Path.Combine(projectOutput, "out.html"); File.WriteAllText(htmlFilename, html); var dirname = Path.GetDirectoryName(htmlFilename); var cssFilename = Path.Combine(dirname, "style.css"); if (!File.Exists(cssFilename)) { File.Copy("style.css", cssFilename); } Console.WriteLine("Your project was successfully converted!"); }