You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utils.cs 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Docnet
  8. {
  9. public static class Utils
  10. {
  11. public static string ConvertMarkdownToHtml(string toConvert, List<Tuple<string, string>> createdAnchorCollector)
  12. {
  13. var parser = new Markdown(new MarkdownOptions() { EmptyElementSuffix = ">"});
  14. var toReturn = parser.Transform(toConvert);
  15. if(createdAnchorCollector != null)
  16. {
  17. createdAnchorCollector.AddRange(parser.CollectedH2AnchorNameTuples);
  18. }
  19. return toReturn;
  20. }
  21. /// <summary>
  22. /// Copies directories and files, eventually recursively. From MSDN.
  23. /// </summary>
  24. /// <param name="sourceFolderName">Name of the source dir.</param>
  25. /// <param name="destinationFolderName">Name of the dest dir.</param>
  26. /// <param name="copySubFolders">if set to <c>true</c> it will recursively copy files/folders.</param>
  27. public static void DirectoryCopy(string sourceFolderName, string destinationFolderName, bool copySubFolders)
  28. {
  29. // Get the subdirectories for the specified directory.
  30. DirectoryInfo sourceFolder = new DirectoryInfo(sourceFolderName);
  31. if(!sourceFolder.Exists)
  32. {
  33. throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceFolderName);
  34. }
  35. DirectoryInfo[] sourceFoldersToCopy = sourceFolder.GetDirectories();
  36. // If the destination directory doesn't exist, create it.
  37. if(!Directory.Exists(destinationFolderName))
  38. {
  39. Directory.CreateDirectory(destinationFolderName);
  40. }
  41. // Get the files in the directory and copy them to the new location.
  42. foreach(FileInfo file in sourceFolder.GetFiles())
  43. {
  44. file.CopyTo(Path.Combine(destinationFolderName, file.Name), false);
  45. }
  46. if(copySubFolders)
  47. {
  48. foreach(DirectoryInfo subFolder in sourceFoldersToCopy)
  49. {
  50. Utils.DirectoryCopy(subFolder.FullName, Path.Combine(destinationFolderName, subFolder.Name), copySubFolders);
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Makes toMakeAbsolute an absolute path, if it's not already a rooted path. If it's not a rooted path it's assumed it's relative to rootPath and is combined with that.
  56. /// </summary>
  57. /// <param name="rootPath">The root path.</param>
  58. /// <param name="toMakeAbsolute">To make absolute.</param>
  59. /// <returns></returns>
  60. public static string MakeAbsolutePath(string rootPath, string toMakeAbsolute)
  61. {
  62. if(string.IsNullOrWhiteSpace(toMakeAbsolute))
  63. {
  64. return rootPath;
  65. }
  66. if(Path.IsPathRooted(toMakeAbsolute))
  67. {
  68. return toMakeAbsolute;
  69. }
  70. var rawToReturn = Path.Combine(rootPath, toMakeAbsolute);
  71. return Path.GetFullPath(rawToReturn).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  72. }
  73. /// <summary>
  74. /// Creates the folders in the path specified if they don't exist, recursively
  75. /// </summary>
  76. /// <param name="fullPath">The full path.</param>
  77. public static void CreateFoldersIfRequired(string fullPath)
  78. {
  79. string folderToCheck = Path.GetDirectoryName(fullPath);
  80. if(string.IsNullOrWhiteSpace(folderToCheck))
  81. {
  82. // nothing to do, no folder to emit
  83. return;
  84. }
  85. if(!Directory.Exists(folderToCheck))
  86. {
  87. Directory.CreateDirectory(folderToCheck);
  88. }
  89. }
  90. /// <summary>
  91. /// Creates a relative path to get from fromPath to toPath. If one of them is empty, the emptystring is returned. If there's no common path, toPath is returned.
  92. /// </summary>
  93. /// <param name="fromPath">From path.</param>
  94. /// <param name="toPath">To path.</param>
  95. /// <returns></returns>
  96. /// <remarks>Only works with file paths, which is ok, as it's used to create the {{Path}} macro.</remarks>
  97. public static string MakeRelativePath(string fromPath, string toPath)
  98. {
  99. var fromPathToUse = fromPath;
  100. if(string.IsNullOrEmpty(fromPathToUse))
  101. {
  102. return string.Empty;
  103. }
  104. var toPathToUse = toPath;
  105. if(string.IsNullOrEmpty(toPathToUse))
  106. {
  107. return string.Empty;
  108. }
  109. if(fromPathToUse.Last() != Path.DirectorySeparatorChar)
  110. {
  111. fromPathToUse += Path.DirectorySeparatorChar;
  112. }
  113. if(toPathToUse.Last() != Path.DirectorySeparatorChar)
  114. {
  115. toPathToUse += Path.DirectorySeparatorChar;
  116. }
  117. var fromUri = new Uri(Uri.UnescapeDataString(Path.GetFullPath(fromPathToUse)));
  118. var toUri = new Uri(Uri.UnescapeDataString(Path.GetFullPath(toPathToUse)));
  119. if(fromUri.Scheme != toUri.Scheme)
  120. {
  121. // path can't be made relative.
  122. return toPathToUse;
  123. }
  124. var relativeUri = fromUri.MakeRelativeUri(toUri);
  125. string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
  126. if(toUri.Scheme.ToUpperInvariant() == "FILE")
  127. {
  128. relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
  129. }
  130. return relativePath;
  131. }
  132. }
  133. }

No Description

Contributors (1)