Commit f95aa496 by Bridger Maxwell

Better compatibility with the Codemirror editor and the markdown circuit extension.

parent 99b8a511
...@@ -5,12 +5,14 @@ Image Circuit Extension for Python-Markdown ...@@ -5,12 +5,14 @@ Image Circuit Extension for Python-Markdown
circuit:name becomes the circuit. circuit:name becomes the circuit.
''' '''
import markdown
import re
import simplewiki.settings as settings import simplewiki.settings as settings
from mitxmako.shortcuts import render_to_response, render_to_string from mitxmako.shortcuts import render_to_response, render_to_string
import markdown
try: try:
# Markdown 2.1.0 changed from 2.0.3. We try importing the new version first, # Markdown 2.1.0 changed from 2.0.3. We try importing the new version first,
# but import the 2.0.3 version if it fails # but import the 2.0.3 version if it fails
...@@ -22,22 +24,40 @@ class CircuitExtension(markdown.Extension): ...@@ -22,22 +24,40 @@ class CircuitExtension(markdown.Extension):
def __init__(self, configs): def __init__(self, configs):
for key, value in configs : for key, value in configs :
self.setConfig(key, value) self.setConfig(key, value)
def add_inline(self, md, name, klass, re): def extendMarkdown(self, md, md_globals):
pattern = klass(re) ## Because Markdown treats contigous lines as one block of text, it is hard to match
## a regex that must occupy the whole line (like the circuit regex). This is why we have
## a preprocessor that inspects the lines and replaces the matched lines with text that is
## easier to match
md.preprocessors.add('circuit', CircuitPreprocessor(md), "_begin")
pattern = CircuitLink(r'processed-schematic:(?P<data>.*?)processed-schematic-end')
pattern.md = md pattern.md = md
pattern.ext = self pattern.ext = self
md.inlinePatterns.add(name, pattern, "<reference") md.inlinePatterns.add('circuit', pattern, "<reference")
class CircuitPreprocessor(markdown.preprocessors.Preprocessor):
preRegex = re.compile(r'^circuit-schematic:(?P<data>.*)$')
def extendMarkdown(self, md, md_globals): def run(self, lines):
self.add_inline(md, 'circuit', CircuitLink, r'^circuit-schematic:(?P<data>.*)$') new_lines = []
for line in lines:
m = self.preRegex.match(line)
if m:
new_lines.append('processed-schematic:{0}processed-schematic-end'.format( m.group('data') ))
else:
new_lines.append(line)
return new_lines
class CircuitLink(markdown.inlinepatterns.Pattern): class CircuitLink(markdown.inlinepatterns.Pattern):
def handleMatch(self, m): def handleMatch(self, m):
data = m.group('data') data = m.group('data')
##TODO: We need to html escape the data ##TODO: We need to html escape the data
return etree.fromstring("<input type='hidden' parts='' value='" + data + "' analyses='' class='schematic ctrls'/>") return etree.fromstring("<input type='hidden' parts='' value='" + data + "' analyses='' class='schematic ctrls' width='150' height='150'/>")
def makeExtension(configs=None) : def makeExtension(configs=None) :
......
...@@ -276,7 +276,6 @@ class Revision(models.Model): ...@@ -276,7 +276,6 @@ class Revision(models.Model):
# Create pre-parsed contents - no need to parse on-the-fly # Create pre-parsed contents - no need to parse on-the-fly
ext = WIKI_MARKDOWN_EXTENSIONS ext = WIKI_MARKDOWN_EXTENSIONS
ext += ["wikipath(base_url=%s)" % reverse('wiki_view', args=('/',))] ext += ["wikipath(base_url=%s)" % reverse('wiki_view', args=('/',))]
print ext
self.contents_parsed = markdown(self.contents, self.contents_parsed = markdown(self.contents,
extensions=ext, extensions=ext,
safe_mode='escape',) safe_mode='escape',)
......
...@@ -980,7 +980,7 @@ var CodeMirror = (function() { ...@@ -980,7 +980,7 @@ var CodeMirror = (function() {
maxWidth = scroller.clientWidth; maxWidth = scroller.clientWidth;
var curNode = lineDiv.firstChild, heightChanged = false; var curNode = lineDiv.firstChild, heightChanged = false;
doc.iter(showingFrom, showingTo, function(line) { doc.iter(showingFrom, showingTo, function(line) {
if (!line.hidden && !line.widgetFunction) { //TODO: We should handle widget blocks better here if (!line.hidden && !line.widgetFunction) {
var height = Math.round(curNode.offsetHeight / th) || 1; var height = Math.round(curNode.offsetHeight / th) || 1;
if (line.widgetFunction) height = line.widgetFunction.size(line.text).height / textHeight(); if (line.widgetFunction) height = line.widgetFunction.size(line.text).height / textHeight();
if (line.height != height) { if (line.height != height) {
......
...@@ -77,6 +77,8 @@ CodeMirror.defineMode("mitx_markdown", function(cmCfg, modeCfg) { ...@@ -77,6 +77,8 @@ CodeMirror.defineMode("mitx_markdown", function(cmCfg, modeCfg) {
update_schematics(); update_schematics();
var schmInput = node.firstChild; var schmInput = node.firstChild;
schmInput.codeMirrorLine = line; schmInput.codeMirrorLine = line;
schmInput.schematic.always_draw_grid = true;
schmInput.schematic.redraw_background();
$(node).leanModal(); $(node).leanModal();
} }
}; };
......
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
mode: 'mitx_markdown', mode: 'mitx_markdown',
matchBrackets: true, matchBrackets: true,
theme: "default", theme: "default",
lineWrapping: false, lineWrapping: true,
}); });
//Store the inital contents so we can compare for unsaved changes //Store the inital contents so we can compare for unsaved changes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment