#!/usr/bin/python
#
# Copyright (C) 2009 Jan Essert
#
# This is shuffle_notation.py, a simple script to permute notation in a mathematical text written in LaTeX
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
import optparse, string, random, sys, re
# LaTeX greek letters
small_greek_letters = [r"\\alpha",r"\\beta",r"\\gamma", r"\\delta", r"\\epsilon", r"\\zeta", \
r"\\eta", r"\\theta", r"\\iota", r"\\kappa", r"\\lambda", r"\\mu", r"\\nu",r"\\xi",r"\\pi", r"\\rho", \
r"\\sigma", r"\\tau", r"\\chi", r"\\phi", r"\\psi", r"\\omega" ]
large_greek_letters = [r"\\Gamma", r"\\Delta", r"\\Theta", r"\\Lambda", r"\\Xi", r"\\Pi", r"\\Sigma", r"\\Phi", r"\\Psi", r"\\Omega"]
# Everything that we permute
all_symbols = list(string.ascii_letters) + small_greek_letters + large_greek_letters
# patterns finding math mode strings
# Result is always (text before, delimiter, math mode text, delimiter, text afterwards)
patterns = [ (r"(?P.*?(?:\$.*?\$(?P=first))*)(\$)([^$]*?)(\$)(.*?)",r"\$"), \
(r"(?P[^$]*(?:\$\$.*?\$\$(?P=first))*)(\$\$)([^$]*?)(\$\$)(.*)",r"\$\$"), \
(r"(.*)((?