How to get the content between two identical elements - dart

I want to parse the root element of such data as follows
<elementA>
...
...anything
...
</ elementA>
<elementB>
<!--anything such as same element name-->
<elementB>hahahha</elementB>
</elementB>
<
elementC
>
{
”aa“: 11,
}
</
elementC >
If I am successful in parsing, a list will be returned with a structure similar to the following
[
"elementA":"...\n...anything\n...",
"elementB":"<!--anything such as same element name-->\n<elementB>hahahha</elementB>",
"elementC":"{\n\”aa\“: 11,\n}",
]
I also refer to this library dart-xml
Here is my core parsing logic
// 元素:空格 + < 空格 + 名 + 空格 + > + 内容 + </ + 空格 + 名 + 空格 + > + 空格)
Parser element() => ref0(spaceOptional)
.seq(RootToken.openElement.toParser())
.seq(ref0(spaceOptional))
.seq(ref0(wrapName))
.seq(ref0(spaceOptional))
.seq(RootToken.closeElement.toParser())
.seq(ref0(content))
.seq(RootToken.openEndElement.toParser())
.seq(ref0(spaceOptional))
.seq(ref0(wrapName))
.seq(ref0(spaceOptional))
.seq(RootToken.closeElement.toParser())
.seq(ref0(spaceOptional));
But I don't know how to parse the content, because the content contains arbitrary characters, such as the same name as the root element。
Also thanks for this library dart-petitparser and the author, thanks.

Your code looks fine, I assume the question is how to define the content production so that it consumes everything nested into the element?
There are multiple ways depending on your exact requirements. The following code recursively parses other elements OR any other character.
Parser content() => (ref0(element) | any()).star();
If you want to study a full blown example have a look at the XML parser on GitHub.

Related

Append to query-string with the same key

I am trying to add some parts to a query-string with the same key.
I have a url like http://server.com/search/result?query=hello with some controls that should add a collaborator to the query-string.
This is done with this:
<g:link action="result" params="${params + ['collaborator': collaborator.id]}">${collaborator.name}</g:link>
Which returns: http://server.com/search/?query=hello&collaborator=<id>.
This is all good, but its possible to choose more collaborators. How do I append another &collaborator=<id> to the query?
I have tried various methods like ${params + [params.collaborator + collaborator.id]}, but this only puts them in one string.
Try this:
gsp:
<g:link action="result" params="${params + ['collaborator': params.list('collaborator') + [collaborator.id]]}">${collaborator.name}</g:link>
action:
List collaboratorList = params.list('collaborator')
//Your operations on this list

How to handle URL relative/absolute "glitch" in contenteditable when copy and pasting

In contenteditable regions, if you paste an element with a URL attribute, in some browsers it converts the URL from relative to absolute.
I've read through some bug reports that claim it's "fixed" in the latest release, but it's not.
I threw together this fiddle to demonstrate: Hurray for Demos!
It's there, it's ugly, and I'm wondering what is the best way to fix it.
The 1st idea that comes to mind is onpaste, find all anchors in the current node and parse it with regex. Not ideal I suppose, but it might be effective.
???
???
I really wish they'd just leave things alone and not create so many browser related issues with contenteditable, but I guess that would make it too easy.
Any thoughts on the best way to address this?
CKEditor, before letting browser break the data, copies all src, name and href attributes to data-cke-saved-src|href attributes. Unfortunately, since data is a string, it has to be done by regexp. You can find the code here: /core/htmldataprocessor.js#L772-L783.
var protectElementRegex = /<(a|area|img|input|source)\b([^>]*)>/gi,
// Be greedy while looking for protected attributes. This will let us avoid an unfortunate
// situation when "nested attributes", which may appear valid, are also protected.
// I.e. if we consider the following HTML:
//
// <img data-x="<a href="X"" />
//
// then the "non-greedy match" returns:
//
// 'href' => '"X"' // It's wrong! Href is not an attribute of <img>.
//
// while greedy match returns:
//
// 'data-x' => '<a href="X"'
//
// which, can be easily filtered out (#11508).
protectAttributeRegex = /([\w-]+)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+))/gi,
protectAttributeNameRegex = /^(href|src|name)$/i;
function protectAttributes( html ) {
return html.replace( protectElementRegex, function( element, tag, attributes ) {
return '<' + tag + attributes.replace( protectAttributeRegex, function( fullAttr, attrName ) {
// Avoid corrupting the inline event attributes (#7243).
// We should not rewrite the existed protected attributes, e.g. clipboard content from editor. (#5218)
if ( protectAttributeNameRegex.test( attrName ) && attributes.indexOf( 'data-cke-saved-' + attrName ) == -1 )
return ' data-cke-saved-' + fullAttr + ' data-cke-' + CKEDITOR.rnd + '-' + fullAttr;
return fullAttr;
} ) + '>';
} );
}
Then, while processing HTML taken from editable element, data-cke-saved-* attributes override the original ones.
This looks like a browser bug that's not specific to contenteditable: https://bugzilla.mozilla.org/show_bug.cgi?id=805359
That issue was opened 10 years ago and last updated 6 years ago. Yet it's still open.
You can see the bug here on StackOverflow. Inspecting any SO link shows that the href value is a relative URL. Copying it and pasting it as HTML has the relative link rewritten into an absolute URL.
Example:

Sublime text 2 Plugin Development - How do I get the language of the current file

== UPDATE ===
So I realized that Sublime already has a command for adding comments. So if I have code inserted like this:
comment = " ----------------------------------------" + '\n'
comment += " " + title + '\n'
comment += " #author " + author + '\n'
comment += " #url " + url + '\n'
comment += " ---------------------------------------" + '\n'
comment = self.view.run_command('toggle_comment')
code = items['code']
layout = comment + code
self.view.replace(edit, sel[0], layout)
How do I get the command to work so that it comments out the comment variable? Thanks.
Initial Question
I am creating a plugin for Sublime Text 2 and want to make sure that when it inserts/replaces code it inserts comments as well, but to do this I need for it to insert the correct comment types for the various languages. I know that I can run the following command:
view.settings().get('syntax')
And that will return something like this:
Packages/Python/Python.tmLanguage
Is there a way to have it return just PHP, Python, C++, etc.
I'm sure I could do a substring command in Python, but since I can see an easy way of seeing all file settings I wanted to make sure there wasn't a quick easy way of doing this. Thanks for the help.
Are you looking for scope_name ?
scope_name(point) | String | Returns the syntax name assigned to the character at the given point.

Sphinx references to other sections containing section number and section title

I am using Sphinx to write a document with lots of references:
.. _human-factor:
The Human Factor
================
...
(see :ref:`human-factor` for details)
The compiled document contains something like this:
(see The Human Factor for details)
Instead I would like to have it formatted like this:
(see 5.1 The Human Factor for details)
I tried to google the solution and I found out that the latex hyperref package can do this but I have no idea how to add this to the Sphinx build.
I resolved it by basically using numsec.py from here: https://github.com/jterrace/sphinxtr
I had to replace the doctree_resolved function with this one to get section number + title (e.g. "5.1 The Human Factor").
def doctree_resolved(app, doctree, docname):
secnums = app.builder.env.toc_secnumbers
for node in doctree.traverse(nodes.reference):
if 'refdocname' in node:
refdocname = node['refdocname']
if refdocname in secnums:
secnum = secnums[refdocname]
emphnode = node.children[0]
textnode = emphnode.children[0]
toclist = app.builder.env.tocs[refdocname]
anchorname = None
for refnode in toclist.traverse(nodes.reference):
if refnode.astext() == textnode.astext():
anchorname = refnode['anchorname']
if anchorname is None:
continue
linktext = '.'.join(map(str, secnum[anchorname]))
node.replace(emphnode, nodes.Text(linktext
+ ' ' + textnode))
To make it work one needs to include the numsec extension in conf.py and also to add :numbered: in the toctree like so:
.. toctree::
:maxdepth: 1
:numbered:

Removing "undefined" in pre populated url form link (from Modified Google Expense Report script)

I've modified the standard google drive expense report system to use it as a competency and experience matrix where the manager can approve/reject a form.
The problem i'm encountering is when a form is rejected by the manager it sends a link back to the employee with a link to a form pre-populated by the url.
However if the employee didn't enter a value in the original form the url populates it as "undefined".
Question, can I pre-populate a form via url removing or replacing the "undefined" values?
Perhaps the code will explain better than I can.
+ "<P><b>" + 'Please re-submit your form <a href=' + USER_FORM_URL +
'&entry_2=' + encodeURIComponent(row.forename) + '&entry_3=' +
encodeURIComponent(row.surname) + '>here</a></b>'
Any help would be much appreciated, struggled to find a solution "out there".
Tom
image link http://s11.postimage.org/dyd1olkoz/url_stack.jpg as i'm a new poster.
Looks like the getObjects() function in the script may skip adding a value to the row object, later used in sendReportToManager(), if the value is empty in the expense report spreadsheet. So, later trying to append something like row.bananas (or something empty in sheet and thus not defined on object) will result in 'undefined' being appended to the string instead. If you look at your querystring for the form with the 'undefined' values, you should see 'undefined' in the url's querystring.
You can avoid this by wrapping your encodeURIComponent(row.XXXX) calls for potentially empty columns to be something like this: row.XXXX ? encodeURIComponent(row.XXXX) : ""
So, replace the likes of:
+ "<P><b>" + 'Please re-submit your form <a href=' + USER_FORM_URL +
'&entry_2=' + encodeURIComponent(row.forename) + '&entry_3=' +
with
+ "<P><b>" + 'Please re-submit your form <a href=' + USER_FORM_URL +
'&entry_2=' + (row.forename ? encodeURIComponent(row.forename) : "") + '&entry_3=' +

Resources