2016-09-05

Parsing fountain files, 2

This is a continuation of my earlier post, Parsing fountain files. In this post, we show how to use Python to parse a fountain file into a latex file using John Pate's screenplay latex class.

First, we need this code:

def fountain2latex(input_fntn_file, name=""):
    """
    Assumes John Pate's screenplay template is loaded.

    INPUT:
       input_fntn_file could be "/home/wdj/my-script.fountain"
       name could be "draft-2016-08-26"

    OUTPUT:
       latex file of script

    EXAMPLE:

    """
    script_list = character_dialog_list(input_fntn_file)[1]
    dones = []
    output_latex_file = input_fntn_file[:-10]+"-"+name+".tex"
    r = open(output_latex_file,"w")
    char_list = character_list(input_fntn_file)
    f = open(input_fntn_file,'r')
    lines = f.readlines()
    N = len(lines)
    for j in range(6):
        x = lines[j]
 if x[:6] == "Title:":
     title = x[6:]
     dones.append(j)
 if x[:7] == "Author:":
     author = x[7:]
     dones.append(j)
 if "Draft date:" in x:
     script_list[j] = "date"
     dones.append(j)
 if "Contact:" in x:
     script_list[j] = "email"
     dones.append(j)
    # start latex header
    r.write("\\documentclass{screenplay}\n")
    #r.write("\\usepackage{article}\n")
    #r.write("\\makeatletter{}\n")
    #r.write("\\g@addto@macro\\quote\\flushleft\n")
    #r.write("\\makeatother\n")
    r.write("\\begin{document}\n")
    r.write("\\title{%s}\n"%title)
    r.write("\\author{%s}\n"%author)
    r.write("\\maketitle\n")
    r.write("\\newpage")
    r.write("\\vskip .2in\n")
    # finish latex header     
    for j in range(6,N):
        x = lines[j]
 if x[0]=="#":
     r.write("%"+x)
     dones.append(j)
 if x[:3].upper() == "INT" and ("DAY" in x or "day" in x):
            r.write("\intslug[day]{"+x[4:-4]+"}")
     dones.append(j)
 if x[:3].upper() == "INT" and ("NIGHT" in x or "night" in x):
            r.write("\intslug[night]{"+x[4:-6]+"}")
     dones.append(j)
 if x[:3].upper() == "EXT" and ("DAY" in x or "day" in x):
            r.write("\extslug[day]{"+x[4:-4]+"}")
     dones.append(j)
 if x[:3].upper() == "EXT" and ("NIGHT" in x or "night" in x):
            r.write("\extslug[night]{"+x[4:-6]+"}")
     dones.append(j)
        for c in char_list:
            if c in x and len(x)==len(c)+1:
         dones.append(j)
         r.write(" \n")
         r.write("\\begin{dialogue}{"+c+"}")
  for i in range(1,N):
      if j+i"#":
   r.write(lines[j+i])
   dones.append(j+i)
      else:
          break
  r.write("\\end{dialogue}")
  r.write(" \n")
 if x[0]==".":
            r.write("\\noindent \n"+x[1:].replace("#","\#")+"\n")
     dones.append(j)
 if not(j in dones):
            r.write(x+"\n")
    f.close()
    # start latex footer
    r.write("\end{document}\n")
    r.close()
    return "finished writing: %s"%output_latex_file

This will parse a large subset of fountain into LaTeX, using John Pate's template. For example, comments must be entered with a "#" at the beginning. Secondary slugs can't be used, "DAY" or "NIGHT" must be at the end of the line. There are other features lacking. None-the-less, Big Fish is rendered as latex which compiles almost without errors:-).

Works fine under linux, which is the main thing for me. I wrote this over the labor day weekend for fun and don't plan to improve it more, but if you have suggestions, I'm happy to hear them.

No comments: