Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

  1. """
  2. Jeffery Russell
  3. 2019-4-21
  4. Simple program to reformat paragraphs in markdown text files
  5. to follow a specific col limit.
  6. """
  7. import os
  8. import sys
  9. def formatChunk(stringContent, charLimit):
  10. """
  11. Formats a markdown section with with a specific
  12. col limit. This only makes changes if the section is a
  13. paragraph and nothing else.
  14. The only guarantee for the input is that there
  15. are no lines which only contain a new line or space.
  16. :param stringContent: string of markdown chunk
  17. :param charLimit: max character limit
  18. :return: formatted string
  19. """
  20. if len(stringContent.strip(" ")) == 0:
  21. return ""
  22. elif not stringContent[0].isalpha():
  23. return stringContent
  24. result = ""
  25. line = ""
  26. stringContent = stringContent.replace("\n", " ")
  27. words = stringContent.split(" ")
  28. for word in words:
  29. if len(line) == 0 or len(line) + len(word) < charLimit:
  30. if not len(line) == 0:
  31. line = line + " "
  32. line = line + word
  33. else:
  34. result = result + line + "\n"
  35. line = word
  36. if line != "":
  37. result = result + line + "\n"
  38. return result
  39. def writeToFile(fileName, content):
  40. """
  41. Writes to a file, overwriting the existing
  42. output.
  43. :param fileName:
  44. :param content:
  45. :return:
  46. """
  47. f = open(fileName, "w+")
  48. f.write(content)
  49. f.close()
  50. def wrapMarkdownParagraphsToColLimit(fileName, charLimit):
  51. """
  52. Breaks apart a markdown file into chunks. A chunks are
  53. separated by a blank lines. Each chunk is then
  54. rendered according to our 80 char rules defined
  55. in the formatChunk section.
  56. :param fileName:
  57. :param charLimit:
  58. :return:
  59. """
  60. result = ""
  61. tempResult = ""
  62. inCodeBlock = False
  63. with open(fileName) as file:
  64. for line in file:
  65. if line.startswith("```"):
  66. inCodeBlock = not inCodeBlock
  67. result = result + line
  68. elif inCodeBlock:
  69. result = result + line
  70. elif line.strip(" ") == "\n":
  71. result = result + formatChunk(tempResult, charLimit)
  72. tempResult = ""
  73. result = result + "\n"
  74. else:
  75. tempResult = tempResult + line
  76. if tempResult != "":
  77. result = result + formatChunk(tempResult, charLimit)
  78. return result
  79. def print_usage():
  80. """
  81. Prints script usage.
  82. :return:
  83. """
  84. print("Usage: file name -t (optional)")
  85. print("\t-p simply prints the formatted output.")
  86. print("\tfile name -- runs the command overwriting the existing file.")
  87. def main():
  88. """
  89. Checks command line input and calls the proper formatting
  90. functions.
  91. :return:
  92. """
  93. if len(sys.argv) > 1:
  94. fileName = os.getcwd() + "/" + sys.argv[1]
  95. if len(sys.argv) == 3 and sys.argv[2].lower() == "-p":
  96. print(wrapMarkdownParagraphsToColLimit(fileName, 70))
  97. else:
  98. writeToFile(fileName,
  99. wrapMarkdownParagraphsToColLimit(fileName, 70))
  100. else:
  101. print_usage()
  102. """
  103. Makes sure that other scripts don't execute the main
  104. """
  105. if __name__ == '__main__':
  106. try:
  107. main()
  108. except KeyboardInterrupt:
  109. panic()