float= The Red Rook (cover).png

Parallel Visions: City of Angels City of Demons ebook and trade paperback available on Amazon (or here).
The question of whether Vance was dead or not became more than academic when he found himself in a bathtub up to his chin in ice water like some forgotten cocktail garnish, a demonic woman standing over him, and no memory of how he got there.
Read free chapters of Parallel Visions: City of Angels City of Demons here
The Red Rook, sequel to Dispensing Justice and the second novel of Nova Genesis World is now available for Kindle or as a paperback at Amazon.
Read free chapters of Dispensing Justice here (or get it here).
Read free chapters of The Red Rook here (or get it here). -- Fritz Freiheit

Dispensing Justice (cover).png 20th Annual Writer's Digest Self-Published Book Awards - Honorable Mention.png

MakeHtmlDocument.py

From FritzWiki
Jump to: navigation, search
http://fritzfreiheit.com/wiki/MakeHtmlDocument.py is best viewed with JavaScript turned on.
 
Article icon.svg
 v • d • e 

Convert a document containing my writing markup to HTML.

# python script; utf-8
#
# makeHtmlDocument.py
#
import os, re, sys
# http://npppythonscript.sourceforge.net/docs/latest/scintilla.html#helper-methods

# run markup transform
editor.pyreplace( r"^\[<\]", "[class='noIndent']" )
editor.pyreplace( r"^\[>\]", "[class='right']" )
editor.pyreplace( r"^\[><\]", "[class='center']" )

# run html markup transform
editor.replace( r'&',   "&amp;" )
editor.replace( r"<",   "&lt;" )
editor.replace( r">",   "&gt;" )
editor.replace( r'"',   "&quot;" )
editor.replace( "0xa0", "&nbsp;" )

def appendNewLine( line ):
    nl = getEOL()
    editor.documentEnd()
    editor.addText( nl + line )

def getEOL():
    mode = editor.getEOLMode()
    # SC_EOL_CRLF (0), SC_EOL_CR (1), or SC_EOL_LF (2)
    if mode == 0: return "\r\n"
    if mode == 1: return "\r"
    if mode == 2: return "\n"
    return "\r\n"

def insertNewLine( lineNumber, line ):
    editor.gotoLine( lineNumber )  
    nl = getEOL()
    editor.addText( line + nl )
    newLineNumber = lineNumber + 1
    return newLineNumber

def stripLeading( leading, line ):
    index = len(leading)
    length = len(line)
    return line[index:length]
    
def makeRestOfLine( leading, line, trailing ):
    line = stripLeading( leading, line )
    line = line.replace( "[/", "<i>" )
    line = line.replace( "/]", "</i>" )
    matchObj = re.match( r'^(.*)((\s|&nbsp;)*\([0-9]+\))$', line )
    # NOTE: need markup for non-breaking space
    if matchObj:
        line = matchObj.group(1) + "<span class=\"pNbr\">" + matchObj.group(2) + "</span>"
    return line + trailing

def makeHtmlDocument( contents, lineNumber, totalLines ):
    line = contents.strip()
    newLine = ""
    if line == "":
        editor.null() # what do we want on blank lines?
    elif line.startswith( "[;]" ):
        newLine = newLine + "<p class='adjusted indent1stLine'>" + makeRestOfLine( "[;]", line, "</p>" )
    elif line.startswith( "[class='center']" ):
        newLine = newLine + "<p class='adjusted center'>" + makeRestOfLine( "[class='center']", line, "</p>" )
    elif line.startswith( "[class='noIndent']" ):
        newLine = newLine + "<p class='adjusted noIndent'>" + makeRestOfLine( "[class='noIndent']", line, "</p>" )
    elif line.startswith( "[class='right']" ):
        newLine = newLine + "<p class='adjusted right'>" + makeRestOfLine( "[class='right']", line, "</p>" )
    elif line.startswith( "[1]" ):
        newLine = newLine + "<h1 class='adjusted'>" + makeRestOfLine( "[1]", line, "</h1>" )
    elif line.startswith( "[2]" ):
        newLine = newLine + "<h2 class='adjusted'>" + makeRestOfLine( "[2]", line, "</h2>" )
    elif line.startswith( "[3]" ):
        newLine = newLine + "<h3 class='adjusted'>" + makeRestOfLine( "[3]", line, "</h3>" )
    else:
        newLine = "<p class='adjusted'>" + line + "</p>"
        
    editor.replaceLine( lineNumber, newLine )
    
    # return 1 # this is the default return
# end of makeHtmlDocument()

editor.forEachLine( makeHtmlDocument )

# now insert trailing html
#notepad.messageBox( "Add closing HTML.", "Make HTML Document", 0 )
appendNewLine( "</body>" )
appendNewLine( "</html>" )

# now insert leading html
#notepad.messageBox( "Add opening HTML.", "Make HTML Document", 0 )
nl = getEOL()
sp = " " * 4
sp2 = sp * 2
lineNumber = 0
lineNumber = insertNewLine( lineNumber, "<html>" )
lineNumber = insertNewLine( lineNumber, "<head>" )
title = notepad.getCurrentFilename()
lineNumber = insertNewLine( lineNumber, sp + "<title>" + title + "</title>" )
# put CSS here
defaultStyles = "margin-top: 0in; margin-bottom: 0in; background: transparent; line-height: 150%;"
paragraphFont = "font-family: \"Droid Serif\", serif;"
headerFont = "font-family: \"Droid Sans\", sans-serif;  font-weight: normal; font-style: normal;"
lineNumber = insertNewLine( lineNumber, sp + "<style type=\"text/css\">" )
lineNumber = insertNewLine( lineNumber, sp2 + "<!--" )
lineNumber = insertNewLine( lineNumber, sp2 + "P { " + defaultStyles + " text-indent: 0.25in; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "P.adjusted { " + paragraphFont + " font-size: 12pt }" )
lineNumber = insertNewLine( lineNumber, sp2 + "P.center { text-indent: 0in; text-align: center; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "P.indent1stLine { text-indent: 0.25in; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "P.noIndent { text-indent: 0in; text-align: left; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "P.right { text-indent: 0in; text-align: right; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "H1 { " + defaultStyles + " text-align: center; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "H1.adjusted { " + headerFont + " font-size: 21pt; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "H2 { " + defaultStyles + " text-align: center; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "H2.adjusted { " + headerFont + " font-size: 16pt; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "H3 { " + defaultStyles + " text-align: center; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "H3.adjusted { " + headerFont + " font-size: 14pt; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "span.pNbr { font-size: 10pt; vertical-align:text-top; color:#999999; }" )
lineNumber = insertNewLine( lineNumber, sp2 + "A:link { color: #000000; text-decoration: none }" )
lineNumber = insertNewLine( lineNumber, sp2 + "A:visited { color: #000000; text-decoration: none }" )
lineNumber = insertNewLine( lineNumber, sp2 + "-->" )
lineNumber = insertNewLine( lineNumber, sp + "</style>" )
lineNumber = insertNewLine( lineNumber, "</head>" )
lineNumber = insertNewLine( lineNumber, "<body>" )

notepad.messageBox( "Make HTML document finished.", "Make HTML Document", 0 )


v • d • e