#! /usr/bin/env python
# -*- coding: utf-8 -*-
#####################      A U T H O R        ##########################
#   Copyright (C) 2010 Jack Desert                                     #
#   jackdesert556@gmail.com                                            #
#   http://www.LetsEATalready.com                                      #
#                                                                      #
######################     L I C E N S E      ##########################
#   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     #
#   of the License, or 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.                                      #
#                                                                      #
########################################################################
#
# LyX(.lyx) --> clipboard(.html)
# This python script takes a LyX file as an input,
# Convertes that to HTML using ElyXer
# And copies the HTML to your clipboard for inserting into blog posts
# Here is an article describing how it came about
# http://letseatalready.com/2010/03/08/lyxblogger-blogging-with-lyx/
# Author: Jack Desert
# Author's Website: http://LetsEATalready.com
# Date: March 14, 2010
# To use LyxBlogger, save
#  this file as /usr/bin/lyxblogger, make it executable with
#  $ chmod +x /usr/bin/lyxblogger
#  Install the following Dependencies:
#  elyxer (version 0.42 or later required for '--raw' option)
#  xclip 0.11 or later (earlier versions do not support utf-8)
#  Then invoke
#  $ lyxblogger my_new_post.lyx

import sys, os
TEMP_DIR = '.temp_latex_665917/'
INPUT_COPY = 'copy_of_input_file.lyx'
error_msg = ''
input_file = sys.argv[1]    # Incoming file name
print 'input_file is:  ' + input_file
# Make sure input file ends in .lyx
if(input_file[-4:] == '.lyx'):
    # Make a temporary directory and work from there
    if (os.path.exists(TEMP_DIR) == False):
        os.system('mkdir %s' % (TEMP_DIR))
    os.system('cp %s %s%s' % (input_file, TEMP_DIR, INPUT_COPY))
    input_path = TEMP_DIR + INPUT_COPY
    OUTPUT_FILE = input_path[0:-4] + '.html'
    if(os.path.exists(input_path)):
        ############## Convert from LyX to HTML##################
        os.system('elyxer --raw --numberfoot %s %s' % (input_path, OUTPUT_FILE))
    else:
        error_msg += 'Error: input_path %s does not exist/n/n' % (input_path)
        print(error_msg)
else:
    error_msg += 'Error: Input file must be of type .lyx/n/n'
    print(error_msg)

# Read data from file
f = open(OUTPUT_FILE, 'r')
html = f.read()
f.close()

# Strip off the beginning and ending tags
START_TAG = '<!--starthtml-->'
END_TAG = '<!--endhtml-->'
if (START_TAG in html):
    html = html.partition(START_TAG)[2]
html = html.partition(END_TAG)[0]

# Reinvoke <code> and </code> tags from their escape sequence counterparts
html = html.replace('&lt;code&gt;', '<code>')
html = html.replace('&lt;/code&gt;', '</code>')

# Remove Arrows from footnotes and margin notes
html = html.replace('[→', '[')
html = html.replace('→]', ']')

# Strip off cut material using the flag '#! CUT MATERIAL'
CUT_FLAG = '\n<div class="Standard">\n#! CUT MATERIAL'
html = html.partition(CUT_FLAG)[0]

# Add tags so that the wordpress plugin 'html-raw' works
html = '<!--start_raw-->' + html + '<!--end_raw-->'

#~ # Write data to file
f = open(OUTPUT_FILE, 'w')

if(error_msg == ''):
    f.write(html)
else:
    f.write(error_msg)
    print '\n\nERROR: \n\nPlease Try Again\n\n'

f.close()
os.system('xclip -i -selection clipboard %s' % (OUTPUT_FILE))

# Clean up
if (os.path.exists(TEMP_DIR)):
    os.system('rm -r %s' % (TEMP_DIR))

