Skip to content

Commit

Permalink
Merge branch 'internship-2016' into integration_autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Barashev committed Sep 11, 2016
2 parents 941fe58 + 98addf4 commit f0b85a0
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 92 deletions.
179 changes: 179 additions & 0 deletions demo/biblio.json
@@ -0,0 +1,179 @@
[
{
"author": [
{
"family": "Rabiner",
"given": "Lawrence R."
}
],
"container-title": "PROCEEDINGS oF tHE iEEE",
"id": "Rabiner89atutorial",
"issued": {
"date-parts": [
[
1989
]
]
},
"page": "257-286",
"title": "A tutorial on hidden markov models and selected applications in speech recognition",
"type": "paper-conference"
},
{
"ISBN": "978-0-549-49012-8",
"author": [
{
"family": "Lee",
"given": "Kyogu"
}
],
"genre": "PhD thesis",
"id": "Lee:2008:SAC:1415439",
"issued": {
"date-parts": [
[
2008
]
]
},
"note": "AAI3302846",
"publisher": "Stanford University",
"publisher-place": "Stanford, CA, USA",
"title": "A system for acoustic chord transcription and key extraction from audio using hidden markov models trained on synthesized audio",
"type": "thesis"
},
{
"author": [
{
"family": "Allen",
"given": "Jont B."
}
],
"container-title": "IEEE Transactions on Acoustics Speech and Signal Processing",
"id": "Allen77short-termspectral",
"issued": {
"date-parts": [
[
1977
]
]
},
"page": "235-238",
"title": "Short-term spectral analysis, and modification by discrete fourier transform",
"type": "article-journal"
},
{
"DOI": "10.1145/1178723.1178727",
"ISBN": "1-59593-501-0",
"URL": "http://doi.acm.org/10.1145/1178723.1178727",
"author": [
{
"family": "Harte",
"given": "Christopher"
},
{
"family": "Sandler",
"given": "Mark"
},
{
"family": "Gasser",
"given": "Martin"
}
],
"collection-title": "AMCMM ’06",
"container-title": "Proceedings of the 1st aCM workshop on audio and music computing multimedia",
"id": "Harte:2006:DHC:1178723.1178727",
"issued": {
"date-parts": [
[
2006
]
]
},
"keyword": "audio, harmonic, music, pitch space, segmentation",
"page": "21-26",
"publisher": "ACM",
"publisher-place": "New York, NY, USA",
"title": "Detecting harmonic change in musical audio",
"type": "paper-conference"
},
{
"author": [
{
"family": "Стругацкий",
"given": "А.Н."
},
{
"family": "Стругацкий",
"given": "Б.Н."
}
],
"editor": [
{
"family": "Иванов"
}
],
"id": "saturday_is_monday",
"issued": {
"date-parts": [
[
1965
]
]
},
"publisher": "Детская литература",
"publisher-place": "М.",
"title": "Понедельник начинается в субботу",
"type": "book"
},
{
"URL": "http://books.google.ru/books?id=xvXuAAAAMAAJ",
"author": [
{
"family": "Кожевников",
"given": "Н.И."
},
{
"family": "Краснощекова",
"given": "Т.И."
},
{
"family": "Шишкин",
"given": "Н.Е."
}
],
"collection-title": "Избранные главы высшей математики для инженеров и студентов втузов. задачи и упражнения",
"id": "book:fourier",
"issued": {
"date-parts": [
[
1964
]
]
},
"publisher": "Наука",
"title": "Ряды и интеграл фурье: Теория поля. аналитические и специальные функции. преобразование лапласа",
"title-short": "Ряды и интеграл фурье",
"type": "book"
},
{
"URL": "http://goo.gl/1eEF3",
"accessed": {},
"author": [
{
"family": "Wikipedia"
}
],
"id": "wiki:lcd",
"issued": {
"date-parts": [
[
2012
]
]
},
"publisher": "Википедия, свободная энциклопедия",
"title": "Наибольший общий делитель",
"type": "webpage"
}
]
6 changes: 2 additions & 4 deletions demo/example.json
@@ -1,5 +1,3 @@
{
"Labels": [
[
{"type": "figure", "caption": "fig:nevergonna"}, {"type": "figure", "caption": "fig:chroma"}, {"type": "figure", "caption": "fig:chord1"}, {"type": "figure", "caption": "fig:chord2"}, {"type": "table", "caption": "my-label"}, {"type": "figure", "caption": "fig:mnb"}, {"type": "figure", "caption": "fig:mnbCrossVal"}, {"type": "figure", "caption": "fig:logisticregression"}, {"type": "figure", "caption": "fig:SVM"}, {"type": "figure", "caption": "fig:sgd"}, {"type": "figure", "caption": "fig:Stacking"}, {"type": "table", "caption": "my-label"}
]
}
]
2 changes: 2 additions & 0 deletions demo/summer-2016.html
Expand Up @@ -303,6 +303,8 @@
var texCompleter = new TexCompleter();
langTools.setCompleters([texCompleter]);
texCompleter.setReferencesUrl("/demo/example.json")
texCompleter.setCitationsUrl("/demo/biblio.json")

console.log("Simple Latex Completer successfully added");
//Initialization bind for highlight brackets.
highlighter.init(ace, editor, {win: 'Ctrl-Shift-9', mac: 'Command-Shift-9'}, candidateToggleSurroundingBracketsPopup);
Expand Down
125 changes: 88 additions & 37 deletions lib/ace/ext/papeeria/tex_completer.coffee
Expand Up @@ -142,42 +142,38 @@ define((require, exports, module) ->
meta_score: 10
)

init = (editor, bindKey) ->
keyboardHandler = new HashHandler.HashHandler()
keyboardHandler.addCommand(
name: "add item in list mode"
bindKey: bindKey
exec: (editor) ->
cursor = editor.getCursorPosition();
line = editor.session.getLine(cursor.row);
tabString = editor.session.getTabString();
indentString = line.match(/^\s*/)[0];
indexOfBegin = line.indexOf("begin")

if LatexParsingContext.getContext(editor.session, cursor.row, cursor.column) == LIST_STATE && indexOfBegin < cursor.column
if indexOfBegin > -1
editor.insert("\n" + tabString + indentString + "\\item ")
else
editor.insert("\n" + indentString + "\\item ")
return true
else
return false
)
editor.keyBinding.addKeyboardHandler(keyboardHandler)

class ReferenceGetter
constructor: ->


processReferenceJson = (elem) =>
return {
name: elem.caption
value: elem.caption
score: 1000
meta: elem.type
meta_score: 10
}

processCitationJson = (elem) =>
return {
name: elem.id
value: elem.id
score: 1000
meta: elem.type
meta_score: 10
}

class CompletionsCache
###
* processJson -- function -- handler for defined type of json(citeJson, refJson, etc)
* return object with fields name, value and (optional) meta, meta_score, score
###
constructor: (processJson) ->
@lastFetchedUrl = ""
@cache = []
@processJson = processJson
processData: (data) =>
@cache = data.Labels?.map((elem) =>
return {
name: elem.caption
value: elem.caption
meta: elem.type + "-ref"
meta_score: 10
}
)
@cache = data?.map(@processJson)

getReferences: (url, callback) =>
if url != @lastFetchedUrl
$.getJSON(url).done((data) =>
Expand All @@ -188,12 +184,64 @@ define((require, exports, module) ->
else
callback(null, @cache)

###
* Show popup if token type at the current pos is one of the given array elements.
* @ (editor) --> editor
* @ (list of strings) -- allowedTypes
###
showPopupIfTokenIsOneOfTypes = (editor, allowedTypes) ->
if editor.completer?
pos = editor.getCursorPosition()
session = editor.getSession()
token = session.getTokenAt(pos.row, pos.column)

if token?
for type in allowedTypes
if LatexParsingContext.isType(token, type)
editor.completer.showPopup(editor)
break

class TexCompleter
constructor: ->
@refGetter = new ReferenceGetter()
@init: (editor) -> init(editor, {win: "enter", mac: "enter"})
@refCache = new CompletionsCache(processReferenceJson)
@citeCache = new CompletionsCache(processCitationJson)
@init: (editor) ->
keyboardHandler = new HashHandler.HashHandler()
keyboardHandler.addCommand(
name: "add item in list mode"
bindKey: {win: "enter", mac: "enter"}
exec: (editor) ->
cursor = editor.getCursorPosition();
line = editor.session.getLine(cursor.row);
tabString = editor.session.getTabString();
indentString = line.match(/^\s*/)[0];
indexOfBegin = line.indexOf("begin")

if LatexParsingContext.getContext(editor.session, cursor.row, cursor.column) == LIST_STATE && indexOfBegin < cursor.column
if indexOfBegin > -1
editor.insert("\n" + tabString + indentString + "\\item ")
else
editor.insert("\n" + indentString + "\\item ")
return true
else
return false
)
editor.keyBinding.addKeyboardHandler(keyboardHandler)

# we need two event handlers because handlers below work fine when we in
# existing \ref{|} or \cite{|}
# But doesn't work correct when we added block (\ref{}. \cite. etc) using autocomplete
editor.commands.on('afterExec', (event) ->
allowCommand = ["Return", "backspace"]
if event.command.name in allowCommand
showPopupIfTokenIsOneOfTypes(editor, ["ref", "cite"])
);

editor.getSession().selection.on('changeCursor', (cursorEvent) ->
showPopupIfTokenIsOneOfTypes(editor, ["ref", "cite"])
);
setReferencesUrl: (url) => @referencesUrl = url
setCitationsUrl: (url) => @citationsUrl = url

###
# callback -- this function is adding list of completions to our popup. Provide by ACE completions API
Expand All @@ -205,8 +253,11 @@ define((require, exports, module) ->
token = session.getTokenAt(pos.row, pos.column)
context = LatexParsingContext.getContext(session, pos.row, pos.column)

if LatexParsingContext.isType(token, "ref") and @referencesUrl?
@refGetter.getReferences(@referencesUrl, callback)
if LatexParsingContext.isType(token, "ref")
if @referencesUrl? then @refCache.getReferences(@referencesUrl, callback)
else if LatexParsingContext.isType(token, "cite")
if @citationsUrl? then @citeCache.getReferences(@citationsUrl, callback)

else switch context
when "start" then callback(null, BASIC_SNIPPETS.concat(LIST_SNIPPET,
EQUATION_ENV_SNIPPETS, REFERENCE_SNIPPET, CITATION_SNIPPET))
Expand Down

0 comments on commit f0b85a0

Please sign in to comment.