NSString remove line breaks (Objective-C) - objective-c

I'm loading data from XML to my NSString, but data is formated like this:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Sed a velit ut nulla tincidunt finibus sit amet sed felis.
Fusce quis quam sapien.
It has line breaks, how can I romeve that so the text looks like this(all in one line):
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a velit ut nulla tincidunt finibus sit amet sed felis. Fusce quis quam sapien. Cras diam metus, viverra in urna sed, efficitur varius sem. Pellentesque accumsan, lectus at rutrum pulvinar,

Try this
NSString *newString = [[oldString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#" "];

Related

PyQuery find() in pandas

I have a pandas dataframe with multiple columns. I am working on a specific column named "Text_annotated" whose structure is like :
Text_annotated
<html> Lorem ipsum dolor sit amet, <phrase>consectetur adipiscing elit</phrase>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <phrase>Ut enim ad minim veniam</phrase>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</html>
<html> Faucibus vitae aliquet nec ullamcorper sit amet risus nullam. Pellentesque sit amet porttitor eget dolor morbi. <phrase>Tincidunt praesent semper feugiat nibh sed pulvinar. Lobortis elementum nibh tellus molestie nunc non blandit.</phrase> Tellus at urna condimentum mattis.</html>
<html>Pulvinar etiam non quam lacus. Amet purus gravida quis blandit. Scelerisque eu ultrices vitae auctor eu augue ut. Tincidunt lobortis feugiat vivamus at augue eget arcu dictum varius. Pellentesque adipiscing commodo elit at imperdiet.</html>
and I want to extract only the text between the <phrase></phrase> tags. For this reason, I decided to use PyQuery. So far I have tried
text_phrases= df['Text_annotated'].tolist()
doc = pq(f"{text_phrases}")
phrase_macro = doc.find("phrase").text()
which returns a pyquery.pyquery.PyQuery where each "newline" contains only one result e.g.
consectetur adipiscing elit
Ut enim ad minim veniam
Tincidunt praesent semper feugiat nibh sed pulvinar. Lobortis elementum nibh tellus molestie nunc non blandit.
Thus, my question is whether it's possible to group the results for each row in the df separated by a comma e.g.
consectetur adipiscing elit, Ut enim ad minim veniam
Tincidunt praesent semper feugiat nibh sed pulvinar. Lobortis elementum nibh tellus molestie nunc non blandit.
(I have also tried to iterate over the objects phrases_res = [h.text() for h in doc('phrase').items()] which didn't work)
Any help/suggestion is much appreciated.
PS. Each row is just wrapped with a <html> tag, without any other particular structure.
EDIT: Tried also to "separate" somehow according to the html tag, but returned the previous result.
rows = doc('html')
for row in rows.text():
phrase_res = doc.find("phrase").text()
new_df['Phrases_res'] = phrase_res
new_df.head(5)
You can use pandas.Series.str.findall with a regex expression to return a list of all the strings between two delimiters.
Try this :
import pandas as pd
pd.options.display.max_colwidth = None
data = ['<html> Lorem ipsum dolor sit amet, <phrase>consectetur adipiscing elit</phrase>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <phrase>Ut enim ad minim veniam</phrase>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</html>',
'<html> Faucibus vitae aliquet nec ullamcorper sit amet risus nullam. Pellentesque sit amet porttitor eget dolor morbi. <phrase>Tincidunt praesent semper feugiat nibh sed pulvinar. Lobortis elementum nibh tellus molestie nunc non blandit.</phrase> Tellus at urna condimentum mattis.</html>',
'<html>Pulvinar etiam non quam lacus. Amet purus gravida quis blandit. Scelerisque eu ultrices vitae auctor eu augue ut. Tincidunt lobortis feugiat vivamus at augue eget arcu dictum varius. Pellentesque adipiscing commodo elit at imperdiet.</html>']
df = pd.DataFrame(data, columns=['Text_annotated'])
df['Phrases'] = df['Text_annotated'].str.findall(r"<phrase>(.*?)</phrase>")
>>> display(df)

Is is possible to load more testdata with liquibase

I want to load a whole html file as testdata with liquibase. So far I've used the approach with loading testdata from csv file, but it is not designed to handle huge html's.
Is there a way to achieve it?
Here is an example to make it clear:
I have a table named Movie. The fields are: id, title, description. In a databaseChangeLog section I point to a file where I store the testdata:
<loadData encoding="UTF-8"
file="config/liquibase/testdata/movie.csv"
separator=";"
tableName="movie"/>
The content is as follows:
id;title;description
1;Titanic;great movie
2;Forrest Gump;another great movie
Now I want to change the description to something that is closer to the real usecase. Lets say this HTML:
<div id="lipsum">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce porta pulvinar lacus eget egestas. Ut quis efficitur turpis. Nunc tincidunt turpis lorem, eget vestibulum nisi sodales at. Quisque in tortor et sapien ornare venenatis. Integer pulvinar nec ipsum malesuada porta. Sed massa metus, condimentum non varius ornare, sollicitudin at dui. Praesent porta, ante et interdum convallis, tellus augue tempus nisl, sit amet mollis augue nisl vel metus.
</p>
<p>
Nam quis libero rhoncus, facilisis magna ut, bibendum urna. Nullam sit amet volutpat turpis. Praesent eget aliquet orci. Duis dignissim tellus erat, eget fermentum augue dapibus sed. Quisque vitae est ipsum. Quisque sit amet libero eget nisi faucibus maximus vel a sem. Proin maximus neque arcu, sit amet eleifend dolor ornare at. Suspendisse laoreet lobortis tellus sed consequat. Nunc commodo ligula eget neque porta consectetur. Mauris sagittis elit in sodales luctus.
</p>
</div>
The content won't fit into the csv file. I could delete all the newlines, but it makes the file unmaintainable.
loadData has a column attribute which in turn has a valueClobFile attribute where you could put in a path to a (html) file.
Checkout these two files in the liquibase integration tests that show a use of this:
batchInsert.changelog.xml
batchInsert.csv

Replace occurrences of a string inside a HTML string using Objective C

I have a HTML string in Objective C. NSString *contentString;
How do I replace the occurrences of a string inside this HTML string while preserving the HTML tags ?
I would use the statement contentString = [contentString stringByReplacingOccurrencesOfString: #"string" withString : #"new string"]; but that will also replace the text within HTML tags. I need to preserve those. I am facing a similar problem while using 3rd party HTML parsers.
The contents of contentString are :
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:epub="http://www.idpf.org/2007/ops" lang="en"><head><link
rel="stylesheet" type="text/css" href="../RESOURCE/core.css" /><link
rel="stylesheet" type="text/css" href="../RESOURCE/inserts.css" /><link
rel="stylesheet" type="text/css" href="../RESOURCE/jquery-ui.css"
/><link rel="stylesheet" type="text/css"
href="../RESOURCE/jquery.qtip.css" /><link type="text/css"
rel="stylesheet" href="../RESOURCE/template-opus.css"
data-wundr-theme="true" /><link type="text/css" rel="stylesheet"
href="../RESOURCE/font-scheme-opus.css?1395092689.864661"
data-wundr-font-scheme="true" /><link type="text/css" rel="stylesheet"
href="../RESOURCE/color-scheme-opus.css?1395092689.864992"
data-wundr-color-scheme="true" /><link type="text/css" rel="stylesheet"
href="../RESOURCE/paragraph-styles.css?1395092689.865252"
data-wundr-paragraph-style="true" /></head><body
data-wundr-page="content"><div class="wundr-guides" style="display:
none;"></div><div class="wundr-page-guide" style="display:
none;"></div><article data-wundr="chapter"
class="wundr-padding-regular"><header><h2
class="pw-par-style-chapter-number pw-par-style-big-letter"
data-wundr-key="chapter-number">1</h2><h1
class="pw-par-style" data-wundr-key="chapter-title"
contenteditable="true"
data-wundr-dummy="true">Chapter</h1></header><section
class="pw-par-style-body" data-wundr-epub-type="subchapter"
contenteditable="true"><p data-wundr-dummy="true">Lorem ipsum dolor sit
amet, consectetur adipiscing elit. Praesent nec mauris lorem. Quisque
luctus velit cursus elit sodales vulputate. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Quisque diam augue, varius vitae auctor eu,
malesuada in purus. Quisque dapibus justo sed nisi gravida malesuada.
Sed consequat enim condimentum lorem semper a iaculis mi consequat.
Donec euismod lobortis est, at tincidunt enim tempor id. Nunc id nunc
quis enim vehicula bibendum sed ut orci. Donec vehicula semper leo, a
auctor tortor bibendum sed. Quisque cursus sapien a eros convallis eget
molestie ligula dignissim. Fusce ullamcorper posuere augue et fermentum.
Integer at dolor sed lorem porttitor iaculis. Sed vitae felis id dolor
vulputate hendrerit ut sed nisl.</p><p data-wundr-dummy="true">Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec mauris
lorem. Quisque luctus velit cursus elit sodales vulputate. Lorem ipsum
dolor sit amet, consectetur adipiscing elit. Quisque diam augue, varius
vitae auctor eu, malesuada in purus. Quisque dapibus justo sed nisi
gravida malesuada. Sed consequat enim condimentum lorem semper a iaculis
mi consequat. Donec euismod lobortis est, at tincidunt enim tempor id.
Nunc id nunc quis enim vehicula bibendum sed ut orci. Donec vehicula
semper leo, a auctor tortor bibendum sed. Quisque cursus sapien a eros
convallis eget molestie ligula dignissim. Fusce ullamcorper posuere
augue et fermentum. Integer at dolor sed lorem porttitor iaculis. Sed
vitae felis id dolor vulputate hendrerit ut sed
nisl.</p></section></article><div></div></body></html>
Use some basic logic (Probably Just a Bool) to check if you are inside or outside of an html tag. Only stringByReplacingOccurrencesOfString if you are outside an html tag. Some of the snippets in this code will help you.
http://www.developerfeed.com/how-remove-html-tags-string-ios
+ (NSString *)flattenHtml: (NSString *) html {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString: html];
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString: #"<" intoString: NULL];
[theScanner scanUpToString: #">" intoString: &text];
// Only Replace if you are outside of an html tag
} // while
return html;
}
Try using some javascript on it after loading it into the web view. That would also allow you to load some tools that could help like jQuery to make the job easier.

PHPStorm: reformat long comments

How do I reformat long (doc) comments in my code? Hitting "reformat" doesn't work. For example when I have this in my code:
/**
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin porta ac urna quis sagittis. Nam risus leo, ultricies
* id ante sed, bibendum rutrum diam.
* Suspendisse viverra dui et ligula aliquet, sit amet mattis magna consequat. Morbi venenatis tempus mattis.
* Praesent mollis quam non turpis laoreet placerat.
*/
How do I turn it into this?
/**
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin porta ac urna quis sagittis. Nam risus leo, ultricies
* id ante sed, bibendum rutrum diam. Suspendisse viverra dui et ligula aliquet, sit amet mattis magna consequat. Morbi
* venenatis tempus mattis. Praesent mollis quam non turpis laoreet placerat.
*/
Such functionality is not currently available in PhpStorm.
But you may try Wrap to Column plugin -- it may do OK for you (seems to work -- based on very quick test).
Related tickets:
http://youtrack.jetbrains.com/issue/WI-374
http://youtrack.jetbrains.com/issue/WI-3713
http://youtrack.jetbrains.com/issue/WI-13469
http://youtrack.jetbrains.com/issue/WI-18264

IOS UIColor in struct

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mauris nibh, venenatis sed purus nec, consequat commodo turpis. Quisque rhoncus maximus mattis. Morbi convallis sagittis lectus eget mollis. Nunc tristique, lectus ac faucibus elementum, dui enim porta ex, nec vestibulum sem neque sit amet est. Donec massa arcu, fermentum a massa eu, maximus rutrum urna. Vestibulum imperdiet pulvinar ipsum, non imperdiet orci efficitur quis. Donec volutpat erat dui, a sodales enim blandit ut. Pellentesque ac imperdiet nibh, sed feugiat lorem. Praesent in velit et est tempus facilisis sed at urna. Nulla sapien sem, sagittis eu mauris quis, consequat tempor sem. Mauris rhoncus, turpis eu vehicula volutpat, nibh libero fermentum quam, vel posuere ante metus quis leo. Maecenas et lorem feugiat neque lobortis accumsan.
You should use the opaque type CGColorRef instead of UIColor on your struct. Then, in the CocoaTouch portion of your code you could get a UIColor instance like this:
UIColor *color = [UIColor colorWithCGColor:colorRef];
I saves you some hassle if you follow that advice of not mixing structs and objects. That leaves you with two possibilities:
Using Objective-C objects: Make "LevelMeterColorThreshold" a class with two properties instead of a struct.
Use C structs and C primitives: Instead of "UIColor" use three floats red/green/blue - if that is sufficient. Then you can reconstruct a UIColor later on.
I probably would pick #1.
How is it possible that in this example: http://developer.apple.com/library/ios/#samplecode/SpeakHere/Listings/AudioViews_LevelMeter_h.html#//apple_ref/doc/uid/DTS40007802-AudioViews_LevelMeter_h-DontLinkElementID_7
they are using the struct with UIColor*?