summaryrefslogtreecommitdiff
path: root/support/comment_io
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/comment_io
Initial commit
Diffstat (limited to 'support/comment_io')
l---------support/comment_io/README1
-rw-r--r--support/comment_io/README.md148
-rw-r--r--support/comment_io/comment_io.py93
3 files changed, 242 insertions, 0 deletions
diff --git a/support/comment_io/README b/support/comment_io/README
new file mode 120000
index 0000000000..42061c01a1
--- /dev/null
+++ b/support/comment_io/README
@@ -0,0 +1 @@
+README.md \ No newline at end of file
diff --git a/support/comment_io/README.md b/support/comment_io/README.md
new file mode 100644
index 0000000000..597ae361cc
--- /dev/null
+++ b/support/comment_io/README.md
@@ -0,0 +1,148 @@
+# Name
+comment_io
+
+
+# Version:
+1.1
+
+
+# Last updated:
+2015-05-14
+
+
+# Author:
+Carl Martin Henrik Larsson, to.martin.larsson@gmail.com
+
+
+# License type:
+GNU Gen­eral Public Li­cense, ver­sion 3
+
+
+# Description:
+comment_io is a small script written in Python to help you to automatically comment and uncomment lines in your code. The program was sprung out of a need I myself had of turning on and off certain environments in LaTeX. After digging around, trying to make this happen by using only LaTeX code, I finally got fed up and just wrote this program instead.
+
+Basically, you use the program by marking certain lines in your code with certain commands. The program then detects these commands and comment or uncomment the whole line depending on which mode you’ve set it to.
+
+
+# Basic usage:
+Place the python file, comment_io.py, in the directory where the file you want to manipulate is located. Then type:
+
+ python comment_io.py 'Name of original file' 'Name of output file' 'Program mode' 'Comment character/s' 'List of commands'
+
+in the terminal. The option parameters are more thoroughly explained below:
+
+* *Name of original file* - The name (or path and name if it’s not placed in the same directory as the program) of the original file that should be manipulated.
+* *Name of output file* - The desired name (or path and name) of the output file that the program generates.
+* *Program mode* - Possible parameters are either ‘in’ or ‘out’. Tells the program whether it should be in in mode (where lines are uncommented) or out mode (where lines are commented out).
+* *Comment character/s* - The comment character/s used in the program language that is being used in the original file.
+* *List of commands* - A list, separated by spaces, of all the commands that should be detected by the program and used to comment out or uncomment lines but without the initial comment character/s.
+
+## Example 1:
+
+ python comment_io.py Test.tex NewFile.tex out % @
+
+Here, all the lines in the file Test.tex that ends with ‘%@‘ (not counting new line characters) will be commented out (by moving ‘%@‘ to the beginning of the line instead). The result is printed to NewFile.tex. That is, if the original file looked like this:
+
+ This is a test. %@
+ This is another test.
+
+The modified file will look like this:
+
+ %@ This is a test.
+ This is another test.
+
+
+## Example 2:
+
+ python comment_io.py Test.tex NewFile.tex in % @
+
+Here, all the lines in the file Test.tex that begins with ‘%@‘ will be uncommented (by moving ‘%@‘ to the end of the line instead, before any new line characters). The result is printed to NewFile.tex. That is, if the original file looked like this:
+
+ %@ This is a test.
+ This is another test.
+
+The modified file will look like this:
+
+ This is a test. %@
+ This is another test.
+
+## Example 3:
+
+ python comment_io.py Test.tex NewFile.tex out % @ optional
+
+Here, all the lines in the file Test.tex that ends with ‘%@‘ or ‘%optional’ (not counting new line characters) will be commented out (by moving ‘%@‘ or ‘@optional’ to the beginning of the line instead). The result is printed to NewFile.tex. That is, if the original file looked like this:
+
+ This is a test. %optional
+ This is another test. %@
+
+The modified file will look like this:
+
+ %optional This is a test.
+ %@ This is another test.
+
+
+# Practical usage
+
+## Example:
+
+In LaTeX, one might sometimes wish to be able to turn on and off certain environments depending on the desired output. While this sometimes can be done within LaTeX itself, other times it can prove to be really hard to get working. For example, let's say that you had a file, 'Test.tex', that looked like this:
+
+ \documentclass{article}
+ \begin{enumerate}
+ \item
+ This is a test.
+ \begin{enumerate}
+ \item
+ This is another test.
+ \end{enumerate}
+ \item
+ This is yet another test.
+ \end{enumerarate}
+ \end{document}
+
+and that you wanted the option to, on the fly, turn of the main enumerate environment but not the nested. This can easily be achived with the help of comment_io. First, just mark all the lines that should be affected, like so:
+
+ \documentclass{article}
+ \begin{enumerate} %remove
+ \item %remove
+ This is a test.
+ \begin{enumerate}
+ \item
+ This is another test.
+ \end{enumerate}
+ \item %remove
+ This is yet another test.
+ \end{enumerarate} %remove
+ \end{document}
+
+Then run:
+
+ python comment_io.py Test.tex NewFile.tex out % remove
+
+This would result in a new file, NewFile.tex, that would look like this:
+
+ \documentclass{article}
+ %remove \begin{enumerate}
+ %remove \item
+ This is a test.
+ \begin{enumerate}
+ \item
+ This is another test.
+ \end{enumerate}
+ %remove \item
+ This is yet another test.
+ %remove \end{enumerarate}
+ \end{document}
+
+Typesetting this file would only output one enumerated item, namely the 'This is another test' part.
+
+
+# Change log
+
+v1.1 (released 2015-06-29):
+* Fixed some bugs.
+* Improved the readme file with useful tips of when to use the program.
+* Corrected some aspect of the readme file that didn't adhere to the Markdown standard.
+
+v1.0 (released 2015-05-12):
+* Added all the basic functionality.
diff --git a/support/comment_io/comment_io.py b/support/comment_io/comment_io.py
new file mode 100644
index 0000000000..7811897f95
--- /dev/null
+++ b/support/comment_io/comment_io.py
@@ -0,0 +1,93 @@
+# Name: CommentIO.py
+# Version 1.1
+# Last updated: 2015-06-29
+# Programmer: Martin Larsson, to.martin.larsson@gmail.com
+# See the readme file for further info.
+
+import sys
+import os
+import os.path
+
+def printHelpMessage(error):
+ print("\nError: " + error + "\n\nUsage: python CommentIO [name of original file] [name of output file] [program mode] [comment character/s] [list of commands]\n\nSee the readme file for further instructions.\n")
+ sys.exit()
+
+# Check if enough arguments have been given.
+if len(sys.argv) < 5:
+ printHelpMessage("To few arguments given.")
+
+# Check if the input file exists.
+if not (os.path.isfile(sys.argv[1]) and os.access(sys.argv[1], os.R_OK)):
+ printHelpMessage("Input file is either missing or is not readable.")
+
+# Check if the program mode option has been specified correctly.
+if not (sys.argv[3] == "in" or sys.argv[3] == "out"):
+ printHelpMessage("The program mode option should either be 'in' or 'out'.")
+
+# Fetch the filename of the input file from system input
+inputFilename = sys.argv[1]
+
+# Fetch the filename of the output file from system input
+outputFilename = sys.argv[2]
+
+# Fetch instructions about whether the program should comment in or comment out, Should be "in" or "out".
+function = sys.argv[3]
+
+# Fetch instructions about which character/s that are used for commenting things out in this language.
+commentCode = sys.argv[4]
+
+# Fetch list of commands that should be used to indicate which lines should be commented in/out.
+listOfCommands = sys.argv[5:]
+
+# Read the file into a list.
+with open(inputFilename) as f:
+ content = f.readlines()
+
+# Go through each line in the list.
+for i, line in enumerate(content):
+ # Ignore the new line character.
+ if line[-1] == "\n":
+ checkLine = line[:-1]
+ else:
+ checkLine = line
+ # Ignore trailing whitespace characters (that includes spaces, tabs, and new line characters).
+ checkLine = checkLine.rstrip()
+ # For each remove command in the list of commands, check to see if the beginign or the ending of the line match the command. In that case, manipulate that line.
+ for j, command in enumerate(listOfCommands):
+ # Check to see if the line should be commented out.
+ if function == "out":
+ # Check to see if the end of the line matches the command.
+ if checkLine[-len(commentCode + command):] == (commentCode + command):
+ # In that case, add the command to the begining of the line instead and remove it from the end.
+ tmp = content[i]
+ if(tmp[len(tmp) - 1] == '\n'):
+ content[i] = tmp.rstrip() + ' ' + commentCode + command + '\n'
+ else:
+ content[i] = tmp.rstrip() + ' ' + commentCode + command
+ # Check to see if the line should be commented in.
+ elif function == "in":
+ # Check to see if the begining of the line matches the command.
+ if checkLine[:len(commentCode + command)] == (commentCode + command):
+ tmp = content[i]
+ # Check to see if the line ends with a new line character.
+ if tmp[len(tmp) - 1] == '\n':
+ # Check to see if the line before the new line character is a space.
+ if tmp[len(tmp) - 2] == ' ':
+ # Insert the command at the end (before the new line character) without a space before.
+ content[i] = tmp[len(commentCode + command):-1] +(commentCode + command) + tmp[-1]
+ else:
+ # Insert the command at the end (before the new line character) with a space before.
+ content[i] = tmp[len(commentCode + command):-1] + ' ' +(commentCode + command) + tmp[-1]
+ else:
+ # Check to see if the line ends with a space.
+ if tmp[len(tmp) - 1] == ' ':
+ # Insert the command at the end without a space before.
+ content[i] = tmp[len(commentCode + command):] + (commentCode + command)
+ else:
+ # Insert the command at the end with a space before.
+ content[i] = tmp[len(commentCode + command):] + ' ' + (commentCode + command)
+
+# Write the list to a new file.
+output = open(outputFilename, "w")
+output.writelines(content)
+output.close()