# This script should really be documented

import codecs
from os import listdir, path
import pypandoc
import sys
from re import sub
import re


# In[ ]:

def get_rst_file_names():
    def ends_with_rst(line):
        if line.endswith('.rst'):
            return True
        else:
            return False
    return filter(ends_with_rst, listdir(path.curdir))


# In[ ]:

def get_lines(file_name):
    with codecs.open(file_name, 'r', 'utf-8') as f:
        lines = f.readlines()
    return lines


# In[ ]:

def save_lines(lines, file_name):
    with codecs.open(file_name, "w", "utf-8") as f:
        f.writelines(lines)


# In[ ]:

def fix_note_indentation(lines):
    for i, line in enumerate(lines):
        if line.startswith('.. note::'):
            counter = i
            while True:
                counter += 1
                try:
                    if lines[counter] == '\n':
                        break
                    else:
                        lines[counter] = '          ' + lines[counter]
                except:
                    break


# In[ ]:

def remove_endswith(lines, exclude_string='#ex\n'):
    new_lines = []
    for line in lines:
        if not line.endswith(exclude_string):
            new_lines.append(line)
    return new_lines


# In[ ]:

def remove_startsswith(lines, exclude_string):
    new_lines = []
    for line in lines:
        if not line.startswith(exclude_string):
            new_lines.append(line)
    return new_lines


# In[ ]:

def remove_empty_block(lines, block_string):
    new_lines = []
    for i, line in enumerate(lines):
        if line.startswith(block_string) and lines[i + 2] == '    \n':
            pass
        elif line.startswith(block_string) and lines[i + 2] == '\n':
            pass