SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SciPost
SciPost
Commits
f3d06ffa
Commit
f3d06ffa
authored
5 years ago
by
Jean-Sébastien Caux
Browse files
Options
Downloads
Patches
Plain Diff
Work on markup autodetection
parent
9075ae0d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
common/forms.py
+8
-1
8 additions, 1 deletion
common/forms.py
common/utils.py
+87
-12
87 additions, 12 deletions
common/utils.py
scipost/static/scipost/ticket-preview.js
+2
-2
2 additions, 2 deletions
scipost/static/scipost/ticket-preview.js
with
97 additions
and
15 deletions
common/forms.py
+
8
−
1
View file @
f3d06ffa
...
@@ -132,9 +132,15 @@ class MarkupTextForm(forms.Form):
...
@@ -132,9 +132,15 @@ class MarkupTextForm(forms.Form):
def
get_processed_markup
(
self
):
def
get_processed_markup
(
self
):
text
=
self
.
cleaned_data
[
'
markup_text
'
]
text
=
self
.
cleaned_data
[
'
markup_text
'
]
# Detect text format
# Detect text format
language
=
detect_markup_language
(
text
)
markup_detector
=
detect_markup_language
(
text
)
language
=
markup_detector
[
'
language
'
]
print
(
'
language: %s
'
%
language
)
print
(
'
language: %s
'
%
language
)
if
markup_detector
[
'
errors
'
]:
return
markup_detector
if
language
==
'
reStructuredText
'
:
if
language
==
'
reStructuredText
'
:
# This performs the same actions as the restructuredtext filter of app scipost
# This performs the same actions as the restructuredtext filter of app scipost
from
io
import
StringIO
from
io
import
StringIO
...
@@ -160,6 +166,7 @@ class MarkupTextForm(forms.Form):
...
@@ -160,6 +166,7 @@ class MarkupTextForm(forms.Form):
'
language
'
:
language
,
'
language
'
:
language
,
'
errors
'
:
warnStream
.
getvalue
()
'
errors
'
:
warnStream
.
getvalue
()
}
}
# at this point, language is assumed to be plain text
from
django.template.defaultfilters
import
linebreaksbr
from
django.template.defaultfilters
import
linebreaksbr
return
{
return
{
'
language
'
:
language
,
'
language
'
:
language
,
...
...
This diff is collapsed.
Click to expand it.
common/utils.py
+
87
−
12
View file @
f3d06ffa
...
@@ -3,6 +3,7 @@ __license__ = "AGPL v3"
...
@@ -3,6 +3,7 @@ __license__ = "AGPL v3"
from
datetime
import
timedelta
from
datetime
import
timedelta
import
re
from
django.core.mail
import
EmailMultiAlternatives
from
django.core.mail
import
EmailMultiAlternatives
from
django.db.models
import
Q
from
django.db.models
import
Q
...
@@ -140,13 +141,65 @@ def detect_markup_language(text):
...
@@ -140,13 +141,65 @@ def detect_markup_language(text):
"""
"""
Detect which markup language is being used.
Detect which markup language is being used.
Possible return values:
This method returns a dictionary containing:
* plain
* reStructuredText
* language
* errors
Language can be one of: plain, reStructuredText
The criteria used are:
* if the ``math`` role or directive is found together with $...$, return error
* if the ``math`` role or directive is found, return ReST
Assumptions:
* MathJax is set up with $...$ for inline, \[...\] for online equations.
"""
"""
rst_headers
=
[
"
####
"
,
"
****
"
,
"
====
"
,
"
----
"
,
"
^^^^
"
,
"
\"\"\"\"
"
,]
# Inline maths
inline_math
=
re
.
search
(
"
\$[^$]+\$
"
,
text
)
if
inline_math
:
print
(
'
inline math: %s
'
%
inline_math
.
group
(
0
))
# Online maths is of the form \[ ... \]
# The re.DOTALL is to also capture newline chars with the . (any single character)
online_math
=
re
.
search
(
r
'
[\\][[].+[\\][\]]
'
,
text
,
re
.
DOTALL
)
if
online_math
:
print
(
'
online math: %s
'
%
online_math
.
group
(
0
))
rst_math
=
'
.. math::
'
in
text
or
'
:math:`
'
in
text
# Normal inline/online maths cannot be used simultaneously with ReST math.
# If this is detected, language is set to plain, and errors are reported.
# Otherwise if math present in ReST but not in/online math, assume ReST.
if
rst_math
:
if
inline_math
:
return
{
'
language
'
:
'
plain
'
,
'
errors
'
:
(
'
Cannot determine whether this is plain text or reStructuredText.
\n
'
'
You have mixed inline maths ($...$) with reStructuredText markup.
'
'
\n\n
Please use one or the other, but not both!
'
)
}
elif
online_math
:
return
{
'
language
'
:
'
plain
'
,
'
errors
'
:
(
'
Cannot determine whether this is plain text or reStructuredText.
\n
'
'
You have mixed online maths (\[...\]) with reStructuredText markup.
'
'
\n\n
Please use one or the other, but not both!
'
)
}
else
:
# assume ReST
return
{
'
language
'
:
'
reStructuredText
'
,
'
errors
'
:
None
}
# reStructuredText header patterns
rst_header_patterns
=
[
"
^#{2,}$
"
,
"
^\*{2,}$
"
,
"
^={2,}$
"
,
"
^-{2,}$
"
,
"
^\^{2,}$
"
,
"
^
\"
{2,}$
"
,]
# See list of reStructuredText directives at
# See list of reStructuredText directives at
# http://docutils.sourceforge.net/0.4/docs/ref/rst/directives.html
# http://docutils.sourceforge.net/0.4/docs/ref/rst/directives.html
# We don't include the math one here since we covered it above.
rst_directives
=
[
rst_directives
=
[
"
attention
"
,
"
caution
"
,
"
danger
"
,
"
error
"
,
"
hint
"
,
"
important
"
,
"
note
"
,
"
tip
"
,
"
attention
"
,
"
caution
"
,
"
danger
"
,
"
error
"
,
"
hint
"
,
"
important
"
,
"
note
"
,
"
tip
"
,
"
warning
"
,
"
admonition
"
,
"
warning
"
,
"
admonition
"
,
...
@@ -156,28 +209,50 @@ def detect_markup_language(text):
...
@@ -156,28 +209,50 @@ def detect_markup_language(text):
"
contents
"
,
"
sectnum
"
,
"
section-autonumbering
"
,
"
header
"
,
"
footer
"
,
"
contents
"
,
"
sectnum
"
,
"
section-autonumbering
"
,
"
header
"
,
"
footer
"
,
"
target-notes
"
,
"
target-notes
"
,
"
replace
"
,
"
unicode
"
,
"
date
"
,
"
class
"
,
"
role
"
,
"
default-role
"
,
"
replace
"
,
"
unicode
"
,
"
date
"
,
"
class
"
,
"
role
"
,
"
default-role
"
,
"
math
"
,
]
]
# See list at http://docutils.sourceforge.net/0.4/docs/ref/rst/roles.html
# See list at http://docutils.sourceforge.net/0.4/docs/ref/rst/roles.html
rst_roles
=
[
rst_roles
=
[
"
emphasis
"
,
"
literal
"
,
"
pep-reference
"
,
"
rfc-reference
"
,
"
emphasis
"
,
"
literal
"
,
"
pep-reference
"
,
"
rfc-reference
"
,
"
strong
"
,
"
subscript
"
,
"
superscript
"
,
"
title-reference
"
,
"
strong
"
,
"
subscript
"
,
"
superscript
"
,
"
title-reference
"
,
"
math
"
,]
]
nr_rst_roles
=
0
nr_rst_headers
=
0
nr_rst_headers
=
0
for
header
in
rst_headers
:
for
header_pattern
in
rst_header_patterns
:
if
header
in
text
:
matches
=
re
.
findall
(
header_pattern
,
text
,
re
.
MULTILINE
)
nr_rst_headers
+=
1
print
(
'
%s matched %d times
'
%
(
header_pattern
,
len
(
matches
)))
nr_rst_headers
+=
len
(
matches
)
nr_rst_directives
=
0
nr_rst_directives
=
0
for
directive
in
rst_directives
:
for
directive
in
rst_directives
:
if
(
'
.. %s::
'
%
directive
)
in
text
:
if
(
'
.. %s::
'
%
directive
)
in
text
:
nr_rst_directives
+=
1
nr_rst_directives
+=
1
nr_rst_roles
=
0
for
role
in
rst_roles
:
for
role
in
rst_roles
:
if
(
'
:%s:`
'
%
role
)
in
text
:
if
(
'
:%s:`
'
%
role
)
in
text
:
nr_rst_roles
+=
1
nr_rst_roles
+=
1
if
(
nr_rst_headers
>
0
or
nr_rst_directives
>
0
or
nr_rst_roles
>
0
):
if
(
nr_rst_headers
>
0
or
nr_rst_directives
>
0
or
nr_rst_roles
>
0
):
return
'
reStructuredText
'
if
inline_math
:
return
'
plain
'
return
{
'
language
'
:
'
plain
'
,
'
errors
'
:
(
'
Cannot determine whether this is plain text or reStructuredText.
\n
'
'
You have mixed inline maths ($...$) with reStructuredText markup.
'
'
\n\n
Please use one or the other, but not both!
'
)
}
elif
online_math
:
return
{
'
language
'
:
'
plain
'
,
'
errors
'
:
(
'
Cannot determine whether this is plain text or reStructuredText.
\n
'
'
You have mixed online maths (\[...\]) with reStructuredText markup.
'
'
\n\n
Please use one or the other, but not both!
'
)
}
else
:
return
{
'
language
'
:
'
reStructuredText
'
,
'
errors
'
:
None
}
return
{
'
language
'
:
'
plain
'
,
'
errors
'
:
None
}
This diff is collapsed.
Click to expand it.
scipost/static/scipost/ticket-preview.js
+
2
−
2
View file @
f3d06ffa
...
@@ -31,14 +31,14 @@ $('#runPreviewButton').on('click', function(){
...
@@ -31,14 +31,14 @@ $('#runPreviewButton').on('click', function(){
$
(
'
#preview-description
'
).
css
(
'
background
'
,
'
#feebce
'
);
$
(
'
#preview-description
'
).
css
(
'
background
'
,
'
#feebce
'
);
$
(
'
#submitButton
'
).
hide
();
$
(
'
#submitButton
'
).
hide
();
$
(
'
#runPreviewButton
'
).
show
();
$
(
'
#runPreviewButton
'
).
show
();
alert
(
"
An error has occurred while processing the
ReStructuredT
ext:
\n\n
"
+
data
.
errors
);
alert
(
"
An error has occurred while processing the
t
ext:
\n\n
"
+
data
.
errors
);
}
}
$
(
'
#preview-description
'
).
html
(
data
.
processed_markup
);
$
(
'
#preview-description
'
).
html
(
data
.
processed_markup
);
let
preview
=
document
.
getElementById
(
'
preview-description
'
);
let
preview
=
document
.
getElementById
(
'
preview-description
'
);
MathJax
.
Hub
.
Queue
([
"
Typeset
"
,
MathJax
.
Hub
,
preview
]);
MathJax
.
Hub
.
Queue
([
"
Typeset
"
,
MathJax
.
Hub
,
preview
]);
},
},
error
:
function
(
data
)
{
error
:
function
(
data
)
{
alert
(
"
An error has occurred while processing the
ReStructuredT
ext.
"
);
alert
(
"
An error has occurred while processing the
t
ext.
"
);
}
}
});
});
$
(
'
#runPreviewButton
'
).
hide
();
$
(
'
#runPreviewButton
'
).
hide
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment