Last active
March 17, 2020 10:47
-
-
Save zxcnasab/1829d1d0b217d521da62c85e3d4805e8 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 HtmlAgilityPack; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
namespace HTML_to_PHP | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Press <Enter> key to continue..."); | |
while (Console.ReadKey().Key != ConsoleKey.Enter) | |
{ | |
Console.WriteLine("Press <Enter> key to continue..."); | |
} | |
var currentDir = Environment.CurrentDirectory; | |
var rootDir = System.IO.Path.GetFullPath($"{currentDir}/live-new/"); | |
if (Directory.Exists(rootDir)) | |
{ | |
//var files = Directory.GetFiles(rootDir); | |
var files = Directory.GetFiles(rootDir, "*.html", SearchOption.AllDirectories); | |
//var files = Directory.GetFileSystemEntries(rootDir, "*.html", SearchOption.AllDirectories); //not working | |
var i = 0; | |
foreach (var file in files) | |
{ | |
var currentSubDir = file.Replace(rootDir, ""); | |
// evaluate the directory level of the current file being modified so that we can add appropriate number of ellipses in the path to navigate to the root dir | |
var dirLevel = Helper.CountSubstring(currentSubDir, "\\"); | |
var navigateDir = dirLevel > 0 ? string.Concat(Enumerable.Repeat("../", dirLevel)) : ""; | |
var htmlDoc = new HtmlDocument(); | |
htmlDoc.LoadHtml(System.IO.File.ReadAllText(file)); | |
var nav = htmlDoc.DocumentNode.SelectSingleNode("//nav");//.SelectNodes("//*[@id=\"my_control_id\"]"); | |
nav.ParentNode.ReplaceChild(HtmlNode.CreateNode($"<?php include('{navigateDir}partials/main-nav.php') ?>"), nav); | |
bool success = false; | |
using (System.IO.StreamWriter newFile = new System.IO.StreamWriter(file.Replace(".html", ".php"), true)) | |
{ | |
try { htmlDoc.Save(newFile); success = true; } | |
catch (Exception) { throw; } | |
////string text = System.IO.File.ReadAllText(file); | |
//string[] lines = //System.IO.File.ReadAllLines(file); | |
//foreach (string line in lines) | |
//{ | |
// // Use a tab to indent each line of the file. | |
// //Console.WriteLine("\n" + line); | |
// newFile.WriteLine(line); | |
//} | |
} | |
if (success) | |
{ | |
//File.Move(file, file.Replace(".html", ".html.bak")); | |
Console.Write("\r{0} Files Updated...", ++i); | |
Thread.Sleep(300); | |
} | |
} | |
} | |
Console.WriteLine("Done..."); | |
Console.ReadKey(); | |
} | |
} | |
static class Helper | |
{ | |
public static int CountSubstring(this string text, string value) | |
{ | |
int count = 0, minIndex = text.IndexOf(value, 0); | |
while (minIndex != -1) | |
{ | |
minIndex = text.IndexOf(value, minIndex + value.Length); | |
count++; | |
} | |
return count; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment