|
Parallel Visions: City of Angels City of Demons ebook and trade paperback available on Amazon (or here). |
![]() |
|||
MakeHtmlDocument.py
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'&', "&" )
editor.replace( r"<", "<" )
editor.replace( r">", ">" )
editor.replace( r'"', """ )
editor.replace( "0xa0", " " )
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| )*\([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 )
- All_html_headers_to_new_file.py
- Clean_Cliche_file.py
- Clean_Text_Of_High_Level_ASCII.py
- EscapeHtml.py
- DoubleSpaceLines.py
- FixLibreOfficeHtmlExport.py
- Lines_Matching_RE.py
- MakeHtmlDocument.py
- PrependWikiIndents.py
- TrimWhiteSpace.py
- (v)
Categories[edit]
- HTML (category)
- Has Data Table (category)
- Has Layout (category)
- Has LayoutPre (category)
- Has Nav (category)
- Has Tags (category)
- Macro (category)
- NotepadPlusPlus (category)
- NotepadPlusPlus Script (category)
- Python (Computer Language) (category)
- Python Script (category)
- Script (category)
- Word Smithy (category)
- Writing (category)
