I found a module to include in the syntax Markmin div, but I can not do the
installation, the module is called Markdiv and attachment to see if anyone
can make it work, or explain how to include html code in Markmin, Thanks
--
?#-------------------------------------------------------------------------------
# Name: markdiv
# Purpose: support for <div>s in formats like MarkMin, MarkDown, Creole
# tested with MarkMin (web2py.com/markmin)
#
# Author: Mirek Zvolsky
#
# Created: 27.09.2011+
# Copyright: (c) 2011
# Licence: MIT/BSD/GPL
#
# Requirements:
#
#-------------------------------------------------------------------------------
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Examples:
import markdiv
markdiv.markdiv('1-----\naaa**bbb**') # <div>aaa**bbb**</div>
markdiv.gbBluePrint = True
markdiv.markdiv('1-----\naaa**bbb**')
# <div class="span-24 last">aaa**bbb**</div>
import markdiv
import gluon.contrib.markmin.markmin2html as markmin2html # requires web2py
markdiv.gs_format = markmin2html.markmin2html
markdiv.markdiv('1-----\naaa**bbb**') # <div><p>aaa<b>bbb</b></p></div>
"""
# requested ->html conversion
gs_format = None # None: no ->html conversion, just convert <div>s
#gs_format = markmin2html.markmin2html
# tested with markmin2html.markmin2html (part of web2py)
# requires: web2py framework installed,
# import gluon.contrib.markmin.markmin2html as markmin2html
# or use something similar for MarkDown, Creole, ..
gbBluePrint = False # False generates plain <div>s, True <div>s for BluePrint
gsLayoutFile = '' # HTML layout file used to generate HTML in MarkDivPage()
gsLayoutIncludeString = '{{include}}' # this will be replaced in gsLayoutFile
# if not gsLayoutFile: (which is default) gsLayoutDefault... strings will apply
gsLayoutDefaultBefore = '<html><head><title></title></head><body>'
gsLayoutDefaultAfter = '</body>'
# <div> is created using N--- (at least 3 dashes, N is <div>level 0..9 or 1..9)
# higher level <div>s are inserted into lower level <div>s
# commands for BluePrint:
# Number 1-24 after dashes makes appropriate <div> width (screen width is 24)
# Line without number after dashes:
# <div> uses rest of screen width (or of lower level <div> width)
# N--- line can end with : followed by class name(s)
def _div_level(psLine, pdByRef):
"""checks a MarkMin(Div) line,
returns <div> level 1..n if it is <div> (line starts N---...), otherwise 0.
"""
if psLine[1:4] == "---": # 3x
try:
iDivLevel = int(psLine[0]) + 1 # 0|1..n -> 1|2..n
except:
iDivLevel = 0
if iDivLevel:
if pdByRef['div_base'] < 0:
pdByRef['div_base'] = iDivLevel - 1 # 0|1
iDivLevel -= pdByRef['div_base'] # 1..n
else:
iDivLevel = 0
return iDivLevel
def _div_open(piLevels, psClasses, piPrepend, piReqWidth, piAppend,
pdByRef, plBlueprintWidths):
"""creates <div> tag"""
# typically levels=1 if all levels are properly in source text
global gbBluePrint
sHtml = ""
iBaseLevel = pdByRef['current_level']
for iI in xrange(piLevels):
bLevelIsExplicit = iI+1 == piLevels
# False, if level is skipped in source
# in such case generate <div> without class=..
if bLevelIsExplicit:
if gbBluePrint:
iFreeWidth = plBlueprintWidths[iBaseLevel+iI] \
- plBlueprintWidths[iBaseLevel+iI+1]
if piReqWidth:
bLast = piPrepend + piReqWidth + piAppend >= iFreeWidth
if bLast:
if piPrepend >= iFreeWidth: # don't generate, because..
piReqWidth = 0 # we need span-N class
elif piPrepend + piReqWidth > iFreeWidth:
piAppend = 0
piReqWidth = iFreeWidth - piPrepend
else:
piAppend = iFreeWidth - piPrepend - piReqWidth
else:
bLast = True
piReqWidth = iFreeWidth
piAppend = piPrepend = 0
if piReqWidth: # tested again, because can be reseted above
plBlueprintWidths[iBaseLevel+iI+1] += \
piReqWidth + piPrepend + piAppend
psClasses = "span-" + str(piReqWidth) + \
(" prepend-" + str(piPrepend) if piPrepend else "") + \
(" append-" + str(piAppend) if piAppend else "") + \
(" last" if bLast else "") + \
(" " + psClasses if psClasses else "")
else: # if bLevelIsExplicit
plBlueprintWidths[iBaseLevel+iI+1] = \
plBlueprintWidths[iBaseLevel+iI]
# seems otherwise explicit level after skipped level
# cannot be generated properly (we mean for BluePrint)
sHtml += "<div" + (' class="' + psClasses + '"'
if bLevelIsExplicit and psClasses else "") + ">"
pdByRef['current_level'] = iBaseLevel + piLevels
return sHtml
def _div_close(piLevels, pdByRef, plBlueprintWidths):
"""creates </div> tag"""
sHtml = ""
iBaseLevel = pdByRef['current_level']
for iI in xrange(piLevels):
sHtml += "</div>"
if plBlueprintWidths[iBaseLevel-iI] == \
plBlueprintWidths[iBaseLevel-iI-1]:
plBlueprintWidths[iBaseLevel-iI] = 0
pdByRef['current_level'] = iBaseLevel - piLevels
return sHtml
def markdiv(psText, pdExtra={}, pdAllowed={}, psSep='p'):
"""main MarkDiv method,
converts symbolic <div>s (to HTML if not empty gs_format)
"""
global gs_format
lBlueprintWidths = [24] + 12*[0]
dByRef = {'div_base': -1, # =unknown yet, will be 0 or 1 (numbered from..)
'current_level': 0}
sHtml = sMarkmin = ""
for sLine in psText.split('\n'):
iDivLevel = _div_level(sLine, dByRef)
if iDivLevel:
if gs_format:
sHtml += gs_format(sMarkmin,pdExtra,pdAllowed,psSep)
else:
sHtml += sMarkmin
sMarkmin = ""
if iDivLevel <= dByRef['current_level']:
sHtml += _div_close(dByRef['current_level'] - iDivLevel + 1,
dByRef, lBlueprintWidths)
sLineLimited = sLine + '::'
sClasses = sLineLimited.split(':',2)[1].strip() # starting from :
global gbBluePrint
if gbBluePrint:
sWidth = sLineLimited[:sLineLimited.find(':')]
sWidth = sWidth[sWidth.rfind('-')+1:]
# in separated command
# (because "-" can be after ":" in class name too)
# we have something like 4<6>2 ... prepend < width > append
if '<' in sWidth:
sPrepend = sWidth[:sWidth.find('<')]
sWidth = sWidth[sWidth.find('<')+1:]
else:
sPrepend = "" # reset, because we are in cycle
if '>' in sWidth:
sAppend = sWidth[sWidth.find('>')+1:]
sWidth = sWidth[:sWidth.find('>')]
else:
sAppend = "" # reset, because we are in cycle
try:
iPrepend = int(sPrepend)
except:
iPrepend = 0
try:
iReqWidth = int(sWidth)
except:
iReqWidth = 0
try:
iAppend = int(sAppend)
except:
iAppend = 0
else: # if gbBluePrint
iPrepend = iReqWidth = iAppend = 0
sHtml += _div_open(iDivLevel - dByRef['current_level'], sClasses,
iPrepend, iReqWidth, iAppend, dByRef, lBlueprintWidths)
else:
sMarkmin += sLine
if gs_format:
sHtml += gs_format(sMarkmin,pdExtra,pdAllowed,psSep)
else:
sHtml += sMarkmin
sHtml += _div_close(dByRef['current_level'], dByRef, lBlueprintWidths)
return sHtml
def include_snippet(psHtml):
"""includes html snippet into html layout defined by gsLayout.. variables
"""
global gsLayoutFile, gsLayoutIncludeString, \
gsLayoutDefaultBefore, gsLayoutDefaultAfter
bStringsNotFile = True
if gsLayoutFile:
try:
sFileContent = filetostr(gsLayoutFile)
bStringsNotFile = False
except:
pass
if bStringsNotFile: # use gsLayoutDefault.. strings instead of gsLayoutFile
sHtml = gsLayoutDefaultBefore + psHtml + gsLayoutDefaultAfter
else: # gsLayoutFile success
sHtml = strextract(sFileContent, gsLayoutIncludeString, psHtml)
return sHtml
def markdivpage(psText, pdExtra={}, pdAllowed={}, psSep='p'):
"""converts psText to the webpage using layout from gsLayout... variables
"""
sHtml = markdiv(psText, pdExtra, pdAllowed, psSep)
return include_snippet(sHtml)
def file2str(psFile, pdExtra={}, pdAllowed={}, psSep='p'):
"""Converts psFile content to the HTML string.
same as markdiv() but source is File instead of String."""
sFileContent = filetostr(psFile)
return markdiv(sFileContent, pdExtra, pdAllowed, psSep)
def file2strpage(psFile, pdExtra={}, pdAllowed={}, psSep='p'):
"""Converts psFile content to the HTML string and make it part of gsLayout..
"""
sHtml = file2str(psFile, pdExtra, pdAllowed, psSep)
return include_snippet(sHtml)
def file2filepage(psFileIn, psFileOut, pdExtra={}, pdAllowed={}, psSep='p'):
"""Converts psFileIn content to the HTML string,
makes it part of layout defined by gsLayout.. variables
and saves the page into psFileOut"""
sHtml = file2strpage(psFileIn, pdExtra, pdAllowed, psSep)
try:
fOutputFile = open(psFileOut, 'wb')
fOutputFile.write(sHtml)
fOutputFile.close()
bOk = True
except:
bOk = False
return bOk
def strextract(psSearched, psExpressionSought, psReplacement=''):
"""Searches psSearched for first occurence of psExpressionSought
and replaces it with psReplacement"""
iExprPosition = psSearched.find(psExpressionSought)
if iExprPosition<0: # not found
return psSearched
else:
return psSearched[:iExprPosition] + psReplacement + \
psSearched[iExprPosition+len(psExpressionSought):]
def filetostr(psFile):
fInputFile = open(psFile, 'rb')
sFileContent = fInputFile.read()
fInputFile.close()
return sFileContent
if __name__ == '__main__':
print "testing:"
print markdiv(
"safsd\n1---------\nxxx\n1-------\nsadasdASAD")
print markdiv(
"safsd\n0---------:big\n1-----4:small\n1-----:middle\nsadasdASAD")
# 0 based numbering is possible too
print markdiv("s**afs**d")
print markdiv(
"1---------------------------------------------\ntop" + \
"\n1---------------------------------------------" + \
"\n2---------------------------5 :small\nleft" + \
"\n2---------------------------1<12>1 :big\nmiddle" + \
"\n2--------------------------- :small\nright" + \
"\n1----------------------------------------------\nbottom")