[svn] codemirror

This commit is contained in:
goatchurch 2009-06-09 18:59:54 +01:00
parent 32b5c7fbb0
commit 17175637dc
6 changed files with 227 additions and 31 deletions

View File

@ -1,20 +1,143 @@
from django import forms
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
import re import re
import os import os
import datetime
import difflib
import troggle.settings as settings import troggle.settings as settings
def index(request, survex_file):
process(survex_file) def ReplaceTabs(stext):
f = open(settings.SURVEX_DATA + survex_file + ".svx", "rb") res = [ ]
a = f.read() nsl = 0
return render_to_response('svxfile.html', {'settings': settings, for s in re.split("(\t|\n)", stext):
'has_3d': os.path.isfile(settings.SURVEX_DATA + survex_file + ".3d"), if s == "\t":
'title': survex_file, res.append(" " * (4 - (nsl % 4)))
'text': unicode(a, "latin1")}) nsl = 0
continue
if s == "\n":
nsl = 0
else:
nsl += len(s)
res.append(s)
return "".join(res)
class SvxForm(forms.Form):
dirname = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
filename = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
datetime = forms.DateTimeField(widget=forms.TextInput(attrs={"readonly":True}))
outputtype = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
code = forms.CharField(widget=forms.Textarea(attrs={"cols":150, "rows":18}))
def GetDiscCode(self):
fname = settings.SURVEX_DATA + self.data['filename'] + ".svx"
if not os.path.isfile(fname):
return None
fin = open(fname, "rb")
svxtext = fin.read().decode("latin1") # unicode(a, "latin1")
svxtext = ReplaceTabs(svxtext).strip()
fin.close()
return svxtext
def DiffCode(self, rcode):
code = self.GetDiscCode()
difftext = difflib.unified_diff(code.splitlines(), rcode.splitlines())
difflist = [ diffline.strip() for diffline in difftext if not re.match("\s*$", diffline) ]
return difflist
def SaveCode(self, rcode):
fname = settings.SURVEX_DATA + self.data['filename'] + ".svx"
if not os.path.isfile(fname):
return False
fout = open(fname, "w")
res = fout.write(rcode.encode("latin1"))
fout.close()
return True
def Process(self):
print "....\n\n\n....Processing\n\n\n"
cwd = os.getcwd()
os.chdir(os.path.split(settings.SURVEX_DATA + self.data['filename'])[0])
os.system(settings.CAVERN + " --log " + settings.SURVEX_DATA + self.data['filename'] + ".svx")
os.chdir(cwd)
fin = open(settings.SURVEX_DATA + self.data['filename'] + ".log", "rb")
log = fin.read()
fin.close()
log = re.sub("(?s).*?(Survey contains)", "\\1", log)
return log
def svx(request, survex_file): def svx(request, survex_file):
# get the basic data from the file given in the URL
dirname = os.path.split(survex_file)[0]
if dirname:
dirname += "/"
nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
outputtype = "normal"
form = SvxForm({'filename':survex_file, 'dirname':dirname, 'datetime':nowtime, 'outputtype':outputtype})
# if the form has been returned
difflist = [ ]
logmessage = ""
message = ""
if request.method == 'POST': # If the form has been submitted...
rform = SvxForm(request.POST) #
if rform.is_valid(): # All validation rules pass (how do we check it against the filename and users?)
rcode = rform.cleaned_data['code']
outputtype = rform.cleaned_data['outputtype']
difflist = form.DiffCode(rcode)
print "ssss", rform.data
if "revert" in rform.data:
pass
if "process" in rform.data:
if not difflist:
message = "OUTPUT FROM PROCESSING"
logmessage = form.Process()
print logmessage
else:
message = "SAVE FILE FIRST"
form.data['code'] = rcode
if "save" in rform.data:
print "sssavvving"
if form.SaveCode(rcode):
message = "SAVVVED"
# we will reload later
else:
message = "FAILED TO SAVE"
form.data['code'] = rcode
if "diff" in rform.data:
form.data['code'] = rcode
#process(survex_file)
if 'code' not in form.data:
form.data['code'] = form.GetDiscCode()
if not difflist:
difflist.append("none")
if message:
difflist.insert(0, message)
svxincludes = re.findall('\*include\s+"?(.*?)(?:\.svx)?"?\s*?\n(?i)', form.data['code'])
vmap = {'settings': settings,
'has_3d': os.path.isfile(settings.SURVEX_DATA + survex_file + ".3d"),
'title': survex_file,
'svxincludes': svxincludes,
'difflist': difflist,
'logmessage':logmessage,
'form':form}
if outputtype == "ajax":
return render_to_response('svxfiledifflistonly.html', vmap)
return render_to_response('svxfile.html', vmap)
def Dsvx(request, survex_file):
svx = open(settings.SURVEX_DATA + survex_file + ".svx", "rb") svx = open(settings.SURVEX_DATA + survex_file + ".svx", "rb")
return HttpResponse(svx, mimetype="text") return HttpResponse(svx, mimetype="text")
@ -40,5 +163,5 @@ def err(request, survex_file):
def process(survex_file): def process(survex_file):
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(os.path.split(settings.SURVEX_DATA + survex_file)[0]) os.chdir(os.path.split(settings.SURVEX_DATA + survex_file)[0])
os.system(settings.CAVERN + " --log " +settings.SURVEX_DATA + survex_file + ".svx") os.system(settings.CAVERN + " --log " + settings.SURVEX_DATA + survex_file + ".svx")
os.chdir(cwd) os.chdir(cwd)

View File

@ -336,4 +336,13 @@ img.thumbnail {
} }
br.clearfloat { br.clearfloat {
clear:both; clear:both;
} }
div.codeframebit
{
border:thin black dotted;
}
.CodeMirror-line-numbers
{
background-color: #bbb;
}

View File

@ -166,7 +166,7 @@ def Parseloghtml01(year, expedition, txt):
tripparas = re.findall("<hr[\s/]*>([\s\S]*?)(?=<hr)", txt) tripparas = re.findall("<hr[\s/]*>([\s\S]*?)(?=<hr)", txt)
for trippara in tripparas: for trippara in tripparas:
s = re.match(u"(?s)\s*(?:<p>)?(.*?)</?p>(.*)$(?i)", trippara) s = re.match(u"(?s)\s*(?:<p>)?(.*?)</?p>(.*)$(?i)", trippara)
assert s, trippara[:100] assert s, trippara[:300]
tripheader, triptext = s.group(1), s.group(2) tripheader, triptext = s.group(1), s.group(2)
mtripid = re.search('<a id="(.*?)"', tripheader) mtripid = re.search('<a id="(.*?)"', tripheader)
tripid = mtripid and mtripid.group(1) or "" tripid = mtripid and mtripid.group(1) or ""
@ -177,7 +177,7 @@ def Parseloghtml01(year, expedition, txt):
tripdate, triptitle, trippeople = tripheader.split("|") tripdate, triptitle, trippeople = tripheader.split("|")
ldate = ParseDate(tripdate.strip(), year) ldate = ParseDate(tripdate.strip(), year)
mtu = re.search('<p[^>]*>(T/?U.*)', triptext) mtu = re.search('<p[^>]*>(T/?U.*)', triptext)
if mtu: if mtu:
tu = mtu.group(1) tu = mtu.group(1)
@ -203,9 +203,9 @@ def Parseloghtml01(year, expedition, txt):
ltriptext = re.sub("</?b>", "'''", ltriptext) ltriptext = re.sub("</?b>", "'''", ltriptext)
#print ldate, trippeople.strip() print ldate, trippeople.strip()
# could includ the tripid (url link for cross referencing) # could includ the tripid (url link for cross referencing)
EnterLogIntoDbase(date = ldate, place = tripcave, title = triptitle, text = ltriptext, trippeople=trippeople, expedition=expedition, logtime_underground=0) EnterLogIntoDbase(date=ldate, place=tripcave, title=triptitle, text=ltriptext, trippeople=trippeople, expedition=expedition, logtime_underground=0)
def Parseloghtml03(year, expedition, txt): def Parseloghtml03(year, expedition, txt):
@ -254,6 +254,7 @@ yearlinks = [
("1995", "1995/log.htm", Parseloghtml01), ("1995", "1995/log.htm", Parseloghtml01),
("1994", "1994/log.htm", Parseloghtml01), ("1994", "1994/log.htm", Parseloghtml01),
("1993", "1993/log.htm", Parseloghtml01), ("1993", "1993/log.htm", Parseloghtml01),
("1992", "1992/log.htm", Parseloghtml01),
] ]
def SetDatesFromLogbookEntries(expedition): def SetDatesFromLogbookEntries(expedition):

View File

@ -12,16 +12,16 @@
<li><b><a href="{% url caveindex %}">List of Caves</a></b></li> <li><b><a href="{% url caveindex %}">List of Caves</a></b></li>
<li><a href="{% url jgtfile aaaa %}">JGT list of files</a> (temporary simple file list and tunnel use)</li> <li><a href="{% url jgtfile aaaa %}">JGT list of files</a> (temporary simple file list and tunnel use)</li>
<li><a href="{% url survey %}">Survey files</a></li> <li><a href="{% url survey %}">Survey files</a></li>
<li><a href="{% url survexindex all %}">Survex directory</a></li> <li><a href="{% url svx all %}">Survex directory</a></li>
<li><a href="{% url expedition 2008 %}">Expedition 2008</a></li> <li><a href="{% url expedition 2008 %}">Expedition 2008</a></li>
<li><a href="{% url expedition 2007 %}">Expedition 2007</a></li> <li><a href="{% url expedition 2007 %}">Expedition 2007</a></li>
<li><a href="{% url expedition 1993 %}">Expedition 1993</a> (earliest parsed)</li> <li><a href="{% url expedition 1992 %}">Expedition 1992</a> (earliest parsed)</li>
</ul> </ul>
<h2>Further work</h2> <h2>Further work</h2>
<p>Julian's work: <p>Julian's work:
<p>parse 1992-1976 logbooks; (esp top 161)</p> <p>parse 1976-1991 logbooks; (esp top 161)</p>
<p>detect T/U on log entries; </p> <p>detect T/U on log entries; </p>
<p>name matching and spelling in survex files; </p> <p>name matching and spelling in survex files; </p>
<p>Improve logbook wikihtml text</p> <p>Improve logbook wikihtml text</p>

View File

@ -2,18 +2,80 @@
{% load survex_markup %} {% load survex_markup %}
{% block title %}{{ title }}{% endblock %} {% block title %}{{ title }}{% endblock %}
{% block head %}
<script src="{{ settings.MEDIA_URL }}js/base.js" type="text/javascript"></script>
<script type="text/javascript" src="{{settings.MEDIA_URL}}js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="{{settings.MEDIA_URL}}js/jquery.form.js"></script>
<script type="text/javascript" src="{{settings.MEDIA_URL}}CodeMirror-0.62/js/codemirror.js"></script>
<script type="text/javascript">
var codeeditor;
$(document).ready(function()
{
codeeditor = CodeMirror.fromTextArea("id_code",
{
parserfile: ["parsesurvex.js"],
stylesheet: "{{settings.MEDIA_URL}}CodeMirror-0.62/css/survexcolors.css",
path: "{{settings.MEDIA_URL}}CodeMirror-0.62/js/",
textWrapping: false,
lineNumbers: true,
indentUnit: 4,
tabMode: "spaces"
});
$("#id_outputtype").val("ajax");
var options =
{
target: '#difflistajax',
beforeSubmit: function() { $("textarea#id_code").value = codeeditor.getCode().length; },
success: function() { codeeditor.focus(); }
};
$('#codewikiform').ajaxForm(options); // bind form using 'ajaxForm'
});
</script>
{% endblock %}
{% block content %} {% block content %}
<H1>{{ title }}</H1> <h1>Survex File: {{ title }} .svx</h1>
<div><a href="{{ settings.SVX_URL }}{{ title }}.svx">Download svx file</a></div> <form id="codewikiform" action="" method="POST">
{% if has_3d %} <div style="display:none">{{form.filename}} {{form.dirname}} {{form.datetime}} {{form.outputtype}}</div>
<div><a href="{{ settings.SVX_URL }}{{ title }}.3d">Download 3d file</a></div> <input type="submit" name="diff" value="Diffy" />
<div><a href="{{ settings.SVX_URL }}{{ title }}.err">Download err file</a></div> <input type="submit" name="save" value="Save"/>
{% else %} <input type="submit" name="revert" value="Revert"/>
<div>Processing failed</div> <input type="submit" name="process" value="Process" title="executes cavern"/>
{% endif %} (Not implemented: <input type="submit" name="svncheckin" value="svn check-in"/>)
<div><a href="{{ settings.SVX_URL }}{{ title }}.log">Download log file</a></div> <div class="codeframebit">{{form.code}}</div>
</form>
{{ text|survex_to_html }}
{% endblock %} <h4>Output</h4>
<div id="difflistajax">
<pre>
{% for diffline in difflist %}{{diffline}}
{% endfor %}
</pre>
{% if logmessage %}
{% if has_3d %}
<p><a href="{{ settings.SVX_URL }}{{ title }}.3d">3d file</a></p>
{% else %}
<p><b>No 3d file</b></p>
{% endif %}
<pre>
LOGMESSAGES
{{logmessage}}
</pre>
{% endif %}
</div>
{% if svxincludes %}
<p><b>Included files:</b>
{% for svxinclude in svxincludes %}
<a href="{{svxinclude}}.svx">{{svxinclude}}</a>
{% endfor %}
</p>
{% endif %}
{% endblock %}

View File

@ -39,10 +39,11 @@ urlpatterns = patterns('',
#(r'^cave/(?P<cave_id>[^/]+)/edit/$', edit_cave), #(r'^cave/(?P<cave_id>[^/]+)/edit/$', edit_cave),
#(r'^cavesearch', caveSearch), #(r'^cavesearch', caveSearch),
url(r'^survex/(.*?)\.index$', views_survex.index, name="survexindex"),
url(r'^cave/(?P<cave_id>[^/]+)/(?P<year>\d\d\d\d)-(?P<qm_id>\d\d)(?P<grade>[ABCDX]?)?$', views_caves.qm, name="qm"), url(r'^cave/(?P<cave_id>[^/]+)/(?P<year>\d\d\d\d)-(?P<qm_id>\d\d)(?P<grade>[ABCDX]?)?$', views_caves.qm, name="qm"),
(r'^survex/(?P<survex_file>.*)\.svx$', svx),
#url(r'^survex/(.*?)\.index$', views_survex.index, name="survexindex"),
url(r'^survex/(?P<survex_file>.*?)\.svx$', svx, name="svx"),
(r'^survex/(?P<survex_file>.*)\.3d$', threed), (r'^survex/(?P<survex_file>.*)\.3d$', threed),
(r'^survex/(?P<survex_file>.*)\.log$', log), (r'^survex/(?P<survex_file>.*)\.log$', log),
(r'^survex/(?P<survex_file>.*)\.err$', err), (r'^survex/(?P<survex_file>.*)\.err$', err),