Is there any way to get specific css using swiftsoup? - swift

Try to get CSS used using swiftsoup
let link = "<div style="background-image:URL(https://xxxx); color:blue">"
link.attr("style");
I can get all style from the div by using code link.attr
But is there any way to get specific style attr? e.g. I want to get background-image value ?
EDIT
Try using regex
let regex = "background-image:(?i)url?[(]"
print(try item.attr("style"))
print("\(try String(item.attr("style").replacingOccurrences(of: regex, with: "", options: .regularExpression)))")
The output shows: https://xxxxxx) -> Hot to add regex to delete ")" base on that regex ?

Related

autocomplete with Materialize - text instead of optional image

I am using Materialize Autocomplete and I wonder if there is a way to use text instead of "optional image". Why? In case the text is not unique then the user will not know which one to choose. It might happen that the options will be names and there might two people with the same name and surname.
When typing my question I found out that I cannot use duplicate entries in data
data: {
"Radek": myself,
"Radek": some other Radek,
"Radoslav": 'http://placehold.it/250x250'
},
js fiddle example
When you look at the source you find the following lines relevant for the images:
autocompleteOption.append('<img src="'+ data[key] +'" class="right circle"><span>'+ key +'</span>');
and
var img = $el.find('img');
$el.html("<span>" + beforeMatch + "<span class='highlight'>" + matchText + "</span>" + afterMatch + "</span>");
$el.prepend(img);
This prevents us from using the image-attribute for anything other than images.
We can insert something like this to trick Materialize
"Radoslav": " style="display: none;">Inserted Text <br><span style="display: none`
but it will just be converted to text resulting in a option equivalent to
"Inserted Text Radoslav": none
So there is sadly nothing to be gained here.
If you are looking to insert a linebreak, however, you can use this answer on How to force Materialize autocomplete text to flow to new line?

Typoscript: how do I add a parameter to all links in the RTE?

I want to add a parameter to all links entered in the RTE by the user.
My initial idea was to do this:
lib.parseFunc_RTE.tags.link {
typolink.parameter.append = TEXT
typolink.parameter.append.value = ?flavor=lemon
}
So for example:
http://domain.com/mypage.php
becomes
http://domain.com/mypage.php?flavor=lemon
which sounds great -- as long as the link does not already have a query string!
In that case, I obviously end up with two question marks in the URL
So for example:
http://domain.com/prefs.php?id=1234&unit=moon&qty=300
becomes
http://domain.com/prefs.php?id=1234&unit=moon&qty=300?flavor=lemon
Is there any way to add my parameter with the correct syntax, depending on whether the URL already has a query string or not? Thanks!
That would be the solution:
lib.parseFunc_RTE.tags.link {
typolink.additionalParams = &flavor=lemon
}
Note that it has to start with an &, typo3 then generates a valid link. The parameter in the link also will be parsed with realURL if configured accordingly.
Edit: The above solution only works for internal links as described in the documentation https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html
The only solution that works for all links that I see is to use a userFunc
lib.parseFunc_RTE.tags.link {
typolink.userFunc = user_addAdditionalParams
}
Then you need to create a php script and include in your TS with:
includeLibs.rteScript = path/to/yourScript.php
Keep in mind that includeLibs is outdated, so if you are using TYPO3 8.x (and probably 7.3+) you will need to create a custom extension with just a few files
<?php
function user_addAdditionalParams($finalTagParts) {
// modify the url in $finalTagParts['url']
// $finalTagParts['TYPE'] is an indication of link-kind: mailto, url, file, page, you can use it to check if you need to append the new params
switch ($finalTagParts['TYPE']) {
case 'url':
case 'file':
$parts = explode('#', $finalTagParts['url']);
$finalTagParts['url'] = $parts[0]
. (strpos($parts[0], '?') === false ? '?' : '&')
. 'newParam=test&newParam=test2'
. ($parts[1] ? '#' . $parts[1] : '');
break;
}
return '<a href="' . $finalTagParts['url'] . '"' .
$finalTagParts['targetParams'] .
$finalTagParts['aTagParams'] . '>'
}
PS: i have not tested the actual php code, so it can have some errors. If you have troubles, try debugging the $finalTagParts variable
Test whether the "?" character is already in the URL and append either "?" or "&", then append your key-value pair. There's a CASE object available in the TypoScript Reference, with an example you can modify for your purpose.
For anyone interested, here's a solution that worked for me using the replacement function of Typoscript. Hope this helps.
lib.parseFunc_RTE.tags.link {
# Start by "replacing" the whole URL by itself + our string
# For example: http://domain.com/?id=100 becomes http://domain.com/?id=100?flavor=lemon
# For example: http://domain.com/index.html becomes http://domain.com/index.html?flavor=lemon
typolink.parameter.stdWrap.replacement.10 {
#this matches the whole URL
search = #^(.*)$#i
# this replaces it with itself (${1}) + our string
replace =${1}?flavor=lemon
# in this case we want to use regular expressions
useRegExp = 1
}
# After the first replacement is done, we simply replace
# the first '?' by '?' and all others by '&'
# the use of Option Split allow this
typolink.parameter.stdWrap.replacement.20 {
search = ?
replace = ? || & || &
useOptionSplitReplace = 1
}
}

Swift 2 parse HTML and find particular nodes

Using the Kanna import I am currently parsing html using the following code:
if let doc = Kanna.HTML(url: NSURL(string: "https://en.wikipedia.org/wiki/Data")!, encoding: NSUTF8StringEncoding) {
// Search for nodes by XPath
for link in doc.xpath("/html/head...") {
primaryDisplay.text!=link.text!
print(link.text)
}
}
}
I was wondering how to identify specific "nodes"(not sure if that is the correct term) in/on a html page to parse the specific data I want...
Here is a image that shows what it is I wanted to know... I think...
A simple way to do what are you finding is using SwiftSoup
Try this:
do{
let html = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<title>Some webpage</title>" +
"</head>" +
"<body>" +
"<p class='normal'>This is the first paragraph.</p>" +
"<p class='special'><b>this is in bold</b></p>" +
"</body>" +
"</html>";
let doc: Document = try SwiftSoup.parse(html)
let els: Elements = try doc.getElementsByClass("special")
let special: Element? = els.first()//get first element
print(try special?.text())//"this is in bold"
print(special?.tagName())//"p"
print(special?.child(0).tag().getName())//"b"
}catch Exception.Error(let type, let message)
{
print("")
}catch{
print("")
}
You should also take a look at xpath/xquery - it is a language specifically intended to traverse and query XML, which makes it applicable to XHTML and well HTML. XHTML is basically well formed HTML.
Assuming you had an xpath/xquery parser installed on your machine, you could...
get a list of all the p elements in the document: //p
get a list of all the p elements having class "special": //p[#class = 'special']
XQuery adds the ability to query documents using a SQL like syntax called FLWOR.
The difficulty in using this or any other parser for html is that often, the HTML is not well formed. That means that every opening tag does not have a closing tag. This makes any kind of parsing somewhat sketchy as the parser may not be able to figure out the hierarchy implied by the HTML.

Mojolicious, Mojo::DOM select tag by contains text

Is there analog ":contains()"(JQuery, JSoup) selector in Mojolicious?
Selector ":contains('text') ~ td + td" work in JQuery and JSoup. How can I convert it to Mojolicious selector?
http://api.jquery.com/contains-selector/
Description: Select all elements that contain the specified text.
version added: 1.1.4jQuery( ":contains(text)" ) text: A string of text
to look for. It's case sensitive.
http://jsoup.org/apidocs/org/jsoup/select/Selector.html
:contains(text) elements that contains the specified text. The search
is case insensitive. The text may appear in the found element, or any
of its descendants.
Mojolicious analog?
Untested, but I would go in the direction of
$dom->find('*')
->grep(sub { $_->all_text =~ /text/ })
->map('following', 'td')
->map('find', 'td')
(if you have something more specific before your :contains, like at least a tag name selector, then replace the * with that, which should greatly help the performance).
Few experiment with hobbs code and I can repeat JQuery, JSoup selector result:
:contains('some string') ~ td + td
Mojo:
$dom
-> find('*')
-> grep(sub { $_ -> text =~ /some string/; })
-> map('following', '~ td + td')
-> flatten;
But, I don't think it's universal and best way to do such select. Just for start.
text
Extract text content from this element only (not including child
elements), smart whitespace trimming is enabled by default.
flatten
Flatten nested collections/arrays recursively and create a new
collection with all elements.

Getting String between two Strings with regular Expressions

I have a long String:
1;#Subject:SW|
vti_parserversion:SR|14.0.0.4762
vti_folderitemcount:IR|0
_Category:SW|
vti_author:SR|SHAREPOINT\\system
_dlc_DocIdItemGuid:SW|8435986b-4ff2-4e03-9879-d15568d88f0b
vti_approvallevel:SR|
vti_categories:VW|
vti_foldersubfolderitemcount:IR|0
vti_modifiedby:SR|SHAREPOINT\\system
vti_assignedto:SR|
Keywords:SW|
_Status:SW|
vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject vti_assignedto Keywords _Status vti_title _Author _Category _dlc_DocId _Comments _dlc_DocIdUrl _dlc_DocIdItemGuid
ContentTypeId:SW|0x01010013CD7B577B3AC84B8467BC0F2B82B30D
_dlc_DocId:SW|5VQNHKQHD5Z4-9-1
vti_cachedtitle:SR|SharePoint 2010 Deployment Guide
vti_title:SR|SharePoint 2010 Deployment Guide
_Author:SW|
_dlc_DocIdUrl:SW|http://blub.com/_layouts/DocIdRedir.aspx?ID=5VQNHKQHD5Z4-9-2, 5VQNHKQHD5Z4-9-1
_Comments:SW|
And I want to parse the DocIdUrl (the String between "_dlc_DocIdUrl:SW|" and ",") , I am using RegexKitLite with: String stringByMatching:regExP
This is my first RegExpression: NSString* regExp = #"(?=\_dlc_DocIdUrl:SW|)(.*?)(?=\,)";
But it gives me: _dlc_DocIdUrl:SW|http://blub.com/_layouts/DocIdRedir.aspx?ID=5VQNHKQHD5Z4-9-2
I only want the URL, how can i solve that?
I am confused!
Here is the regex solving your problem:
_dlc_DocIdUrl:SW\|(?<value>[^,]*),+
You will find the requested URL in the group named "value"
(?<=_dlc_DocIdUrl:SW\|)[^,]*(?=,)
this give you what you need directly, without grouping.