As mentioned, I want to put two RichTextBlocks one after another vertically. My intention is to write manually the paragraphs of the first RichTextBlock, and I want the second RichTextBlock (that its content given programmatically) to start right after the first one, and I want that to happen so that the second RichTextBlock will overflow vertically to the next column.
The below snippet intended just so you could see the objects I'm using and has no any logical meaning.
<ScrollViewer
Grid.Column="1"
Grid.Row="1"
Grid.RowSpan="2"
HorizontalScrollMode="Auto"
HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Disabled"
VerticalScrollBarVisibility="Disabled"
Padding="20">
<RichTextBlock>
<Paragraph><Run></Run></Paragraph>
<RichTextBlock />
<common:RichTextColumns>
<common:RichTextColumns.ColumnTemplate>
<DataTemplate>
<RichTextBlockOverflow Margin="80,0,0,0" Width="560">
<RichTextBlockOverflow.RenderTransform>
<TranslateTransform X="-1" Y="4"/>
</RichTextBlockOverflow.RenderTransform>
</RichTextBlockOverflow>
</DataTemplate>
</common:RichTextColumns.ColumnTemplate>
<RichTextBlock
htmlToXaml:Properties.Html="{Binding HtmlText}"
Name="RichTextBlockMy"
Margin="10,0,0,0"
Style="{StaticResource ItemRichTextStyle}"
Width="560"/>
</common:RichTextColumns>
</ScrollViewer>
I think this is what you want.
XAML
<ScrollViewer
HorizontalScrollMode="Auto"
HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Disabled"
VerticalScrollBarVisibility="Disabled"
Padding="20">
<StackPanel Orientation="Horizontal">
<RichTextBlock>
<Paragraph>
<Run Text="This is fixed RichTextBlock, after this it's RichTextBlock with RichTextColumns" FontSize="20"/>
<LineBreak />
<Run Text="{Binding FirstRtbText}" />
<LineBreak />
<Run Text="End of fixed RichTextBlock" FontSize="20" />
</Paragraph>
</RichTextBlock>
<common:RichTextColumns>
<common:RichTextColumns.ColumnTemplate>
<DataTemplate>
<RichTextBlockOverflow Margin="80,0,0,0" Width="560">
<RichTextBlockOverflow.RenderTransform>
<TranslateTransform X="-1" Y="4"/>
</RichTextBlockOverflow.RenderTransform>
</RichTextBlockOverflow>
</DataTemplate>
</common:RichTextColumns.ColumnTemplate>
<RichTextBlock
common:Properties.Html="{Binding HtmlText}"
Name="RichTextBlockMy"
Margin="10,0,0,0"
Style="{StaticResource ItemRichTextStyle}"
Width="560"/>
</common:RichTextColumns>
</StackPanel>
</ScrollViewer>
C#
public string HtmlText { get; set; }
public string FirstRtbText { get; set; }
public MainPage()
{
this.InitializeComponent();
FirstRtbText = #"<h1>Header One</h1>
<p>Lorem ipsum <em>emphasised text</em> dolor sit amet, <strong>strong text</strong>
consectetur adipisicing elit, <abbr title="""">abbreviated text</abbr> sed do eiusmod tempor
<acronym title="""">acronym text</acronym> incididunt ut labore et dolore magna aliqua. Ut
<q>quoted text</q> enim ad minim veniam, quis nostrud exercitation link text
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute <del>deleted text</del>
<ins>inserted text</ins> irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat <code>code text</code> cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<blockquote>
<p>Blockquote. Velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia</p>
</blockquote>";
HtmlText = #"<h1>Header One</h1>
<p>Lorem ipsum <em>emphasised text</em> dolor sit amet, <strong>strong text</strong>
consectetur adipisicing elit, <abbr title="""">abbreviated text</abbr> sed do eiusmod tempor
<acronym title="""">acronym text</acronym> incididunt ut labore et dolore magna aliqua. Ut
<q>quoted text</q> enim ad minim veniam, quis nostrud exercitation link text
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute <del>deleted text</del>
<ins>inserted text</ins> irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat <code>code text</code> cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<blockquote>
<p>Blockquote. Velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia</p>
</blockquote>
<p><cite>Cite author with link text</cite>, 2008</p>
<h2>Header 2</h2>
<p>Extended paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<ol>
<li>Ordered list</li>
<li>Item 2 Consectetur adipisicing elit</li>
<li>Item 3 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua</li>
<li>Item 4</li>
<li>Item 5</li>
</ol>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p>
<h3>Header 3</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<ul>
<li>Unordered list</li>
<li>Consectetur adipisicing elit</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<p>Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<pre><code>pre and code pair{
display:block;
line-height:1.833em;
border-top:0.083em solid #200;
}</code></pre>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p>
<h4>Header 4</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<dl>
<dt>Definition list</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
<dt>Lorem ipsum dolor sit amet</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
<dt>Lorem ipsum dolor sit amet</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
<dt>Lorem ipsum dolor sit amet</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
</dl>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<table summary=""Table summary"">
<caption>Table Caption</caption>
<thead>
<tr>
<th>Header</th><th>Header</th><th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content</td><td>1</td><td>a</td>
</tr>
<tr>
<td>Content</td><td>2</td><td>b</td>
</tr>
<tr>
<td>Content</td><td>3</td><td>c</td>
</tr>
<tr>
<td>Content</td><td>4</td><td>d</td>
</tr>
<tr>
<td>Content</td><td>5</td><td>e</td>
</tr>
<tr>
<td>Content</td><td>6</td><td>f</td>
</tr>
</tbody>
</table>
<address>Author text</address>
";
this.DataContext = this;
}
Related
How to highlight text when click label get the index position and highlight One Sentence in xamarin forms.
This is my layout looks llike
<StackLayout>
<Label Text="{Binding Loremipsum.Content}"
x:Name="LoremipsumText"
FontSize="18"
FontFamily="Font4"
TextTransform="None"
TextColor="White">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="btnHighLight"></TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</StackLayout>
See this picture for reference
if the image is not load
this is the text example
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
iwant to hilght is this text
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
and this is my cs codes look likes
public partial class LoremipsumInfoPage : ContentPage
{
string[] strList;
public LoremipsumInfoPage()
{
InitializeComponent();
string verses = Loremipsum.Text;
string strDot = ".";
char charDot= char.Parse(strDot);
string strComma = ",";
char charComma = char.Parse(strComma);
strList = verses.Split(new char[] { charDot, charComma });
}
private void btnHighLight(object sender, EventArgs e)
{
var formattedString = new FormattedString();
for (int v = 0; v < strList.Length; v++)
{
if (v == v)
{
formattedString.Spans.Add(new Span { Text = strList[v], ForegroundColor = Color.White, BackgroundColor = Color.Red });
}
else
{
formattedString.Spans.Add(new Span { Text = strList[v], ForegroundColor = Color.Black });
}
}
Loremipsum.FormattedText = formattedString;
}
}
As ToolmakerSteve said,you can set the formattedText of label with span.I write a sample you can refer to:
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="This is a text" />
<Span x:Name="hlText" Text="This is a highlighttext"/ >
</FormattedString>
</Label.FormattedText>
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapped" />
</Label.GestureRecognizers>
</Label>
codebehind:
void OnTapped(object Sender,Eventargs e)
{hlText.color=Color.Red
}
This question already has answers here:
sed replace empty line with character
(5 answers)
Closed 3 years ago.
Hi I am having a text file . the file has some lines without text . I would like to replace these lines with some specific text .
log.txt
{"id":0,"manufacturer":"cheverlot","model":"AC 3000ME 1002","description":"Culpa exercitation nulla amet excepteur amet sint voluptate elit consectetur. Sit eiusmod velit occaecat consequat pariatur mollit cupidatat ad velit ipsum reprehenderit ea labore aliquip. Nostrud ullamco minim nulla in ea nulla nisi Lorem laboris aute.\r\n","date":"3202-07-20T12:29:26 -01:00"}
{"id":1,"manufacturer":"Bugati","model":"AC 3000ME 1003","description":"Eiusmod magna laborum nulla labore esse. Qui velit nulla eiusmod excepteur anim veniam cupidatat proident incididunt proident non laborum irure aliqua. Magna exercitation eiusmod ad mollit pariatur commodo. Deserunt mollit Lorem do laboris pariatur reprehenderit consequat consectetur excepteur nostrud.\r\n","date":"11925-03-15T05:54:00 -00:00"}
{"id":2,"manufacturer":"Bugati","model":"AC 3000ME 1001","description":"Commodo cupidatat laborum aliquip mollit irure reprehenderit ipsum cillum officia labore Lorem aliqua reprehenderit. Lorem ad consectetur anim aute non tempor magna aliquip elit minim. Et laboris tempor velit non. Commodo amet laborum pariatur id et Lorem consectetur elit cupidatat aute eu proident quis nostrud. Do laboris ipsum officia pariatur deserunt ullamco qui. Eu tempor irure consectetur officia adipisicing cupidatat laboris non consectetur ad laboris commodo deserunt tempor.\r\n","date":"7679-11-22T08:16:34 -00:00"}
{"id":3,"manufacturer":"cheverlot","model":"AC 3000ME 1001","description":"Deserunt occaecat laboris laborum cupidatat nisi reprehenderit aute aute culpa. Sunt consequat fugiat dolor dolore dolor sint. Exercitation sunt adipisicing nostrud culpa non consequat anim est excepteur deserunt et excepteur. Minim eu cupidatat adipisicing quis occaecat deserunt magna ea.\r\n","date":"3959-09-20T09:24:42 -01:00"}
{"id":4,"manufacturer":"Audi","model":"AC 3000ME 1003","description":"Magna commodo fugiat ea consequat incididunt. Adipisicing commodo duis consequat officia esse et ea excepteur exercitation anim laboris dolor ad. Officia magna incididunt irure sit et minim adipisicing aliquip officia magna Lorem qui veniam quis. Lorem elit et deserunt exercitation nisi sit non aliqua officia veniam consequat. Non ut fugiat nisi nulla exercitation nostrud. Ut culpa reprehenderit mollit commodo officia irure labore fugiat cillum tempor esse aliqua est ipsum.\r\n","date":"8024-05-26T04:22:34 -01:00"}
Expected result
{"index":{}}
{"id":0,"manufacturer":"cheverlot","model":"AC 3000ME 1002","description":"Culpa exercitation nulla amet excepteur amet sint voluptate elit consectetur. Sit eiusmod velit occaecat consequat pariatur mollit cupidatat ad velit ipsum reprehenderit ea labore aliquip. Nostrud ullamco minim nulla in ea nulla nisi Lorem laboris aute.\r\n","date":"3202-07-20T12:29:26 -01:00"}
{"index":{}}
{"id":1,"manufacturer":"Bugati","model":"AC 3000ME 1003","description":"Eiusmod magna laborum nulla labore esse. Qui velit nulla eiusmod excepteur anim veniam cupidatat proident incididunt proident non laborum irure aliqua. Magna exercitation eiusmod ad mollit pariatur commodo. Deserunt mollit Lorem do laboris pariatur reprehenderit consequat consectetur excepteur nostrud.\r\n","date":"11925-03-15T05:54:00 -00:00"}
{"index":{}}
{"id":2,"manufacturer":"Bugati","model":"AC 3000ME 1001","description":"Commodo cupidatat laborum aliquip mollit irure reprehenderit ipsum cillum officia labore Lorem aliqua reprehenderit. Lorem ad consectetur anim aute non tempor magna aliquip elit minim. Et laboris tempor velit non. Commodo amet laborum pariatur id et Lorem consectetur elit cupidatat aute eu proident quis nostrud. Do laboris ipsum officia pariatur deserunt ullamco qui. Eu tempor irure consectetur officia adipisicing cupidatat laboris non consectetur ad laboris commodo deserunt tempor.\r\n","date":"7679-11-22T08:16:34 -00:00"}
{"index":{}}
{"id":3,"manufacturer":"cheverlot","model":"AC 3000ME 1001","description":"Deserunt occaecat laboris laborum cupidatat nisi reprehenderit aute aute culpa. Sunt consequat fugiat dolor dolore dolor sint. Exercitation sunt adipisicing nostrud culpa non consequat anim est excepteur deserunt et excepteur. Minim eu cupidatat adipisicing quis occaecat deserunt magna ea.\r\n","date":"3959-09-20T09:24:42 -01:00"}
{"index":{}}
{"id":4,"manufacturer":"Audi","model":"AC 3000ME 1003","description":"Magna commodo fugiat ea consequat incididunt. Adipisicing commodo duis consequat officia esse et ea excepteur exercitation anim laboris dolor ad. Officia magna incididunt irure sit et minim adipisicing aliquip officia magna Lorem qui veniam quis. Lorem elit et deserunt exercitation nisi sit non aliqua officia veniam consequat. Non ut fugiat nisi nulla exercitation nostrud. Ut culpa reprehenderit mollit commodo officia irure labore fugiat cillum tempor esse aliqua est ipsum.\r\n","date":"8024-05-26T04:22:34 -01:00"}
so in short i want to replace those blank lines with this text {"index":{}}
any idea how can i do it in shell script .
thank you
I found the answer
awk '!NF{$0="{\"index\":{}}"}1' log.txt
In my Laravel 5.6/“vue”: "^2.5.7/“vuetify”: “^1.0.8” application I search a sample of image floating left
and text at right with card implementation.
I searched here and did not find…
Could you please give a ref to example?
MODIFIED :
I mean html code like with long text:
<div class="card">
<div class="col-xs-12">
<a class="a_link" target="_blank" href="/storage/votes/-vote-14/mammals.jpg">
<img class=" pull-left img-medium-preview" src="/storage/votes/-vote-14/mammals.jpg" alt="Which is the tallest mammal?" width="423" height="280">
</a>
<div>
<h5 class="card-title">Which is the tallest mammal?</h5>
<p class="card-text">
Which is the tallest mammal lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</div>
</div>
and it looks like that with text floating the image : https://imgur.com/a/KT4Xg3n
Thanks!
Vuetify has a class left which applies float: left !important.
We can use that on the img (but not if it's inside a container which has display: flex), and then the text will wrap around the image.
codepen
And add some style to make it act responsively:
img {
max-width: 100%;
height: auto;
}
In vuetify version ^1.2.0 You can use v-img component which makes it easier to place images.
v-card and it's child components don't have specific props for this behavior afaik, so you can use arbitrary layout inside the card.
<v-card>
<v-layout>
<v-flex xs5>
<v-img src="https://cdn.vuetifyjs.com/images/cards/desert.jpg"></v-img>
</v-flex>
<v-flex xs7>
<v-card-title primary-title>
<div>
Text
</div>
</v-card-title>
</v-flex>
</v-layout>
</v-card>
Then take a look at v-img props and style the image as you see fit.
codepen
If you are not using v-img then you have to use custom CSS for styling your image.
I want to expand the image so that image is 100% width with the browser window with no gutter. It seems adding row expanded to the image does not do the job. Also I need to figure this out by only editing the css.
.wideimage{
min-width: 100vh;
}
<div class="row small-collapse">
<div class="medium-11 medium-centered column">
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
<div class="degutter wideimage"><img src="http://placehold.it/2400x500"></div>
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
</div>
</div>
https://jsfiddle.net/sLk0jf4L/835/
This works. I know it's a pain because you have to do the two rows, with the full width div between. But there are no gutter edges. Like so:
.wideimage{
width: 100%;
min-width: 100vh;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.0.0/foundation.min.css" rel="stylesheet"/>
<div class="row small-collapse">
<div class="medium-11 medium-centered column">
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
</div>
</div>
<div class="degutter wideimage"><img src="http://placehold.it/2400x500"></div>
<div class="row small-collapse">
<div class="medium-11 medium-centered column">
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
</div>
</div>
Like the title says. Is it possible to use an if/then statement in a here-doc for ruby.
Thanks
Yes
irb(main):001:0> x = <<-E
irb(main):002:0" How
irb(main):003:0" now
irb(main):004:0" #{
irb(main):005:0" if true
irb(main):006:0" "brown"
irb(main):007:0" else
irb(main):008:0" "red"
irb(main):009:0" end
irb(main):010:0" } cow
irb(main):011:0" E
=> "How\nnow\nbrown cow\n"
irb(main):012:0> puts x
How
now
brown cow
Compare (and contrast) these samples. Which is more readable, has clearer logic, and would you want to support?:
Example #1:
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
#{
if (true_or_false)
'jackdaws love my giant sphinx of quartz'
else
'Sphinx of black quartz, judge my vow.'
end
}
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Example #2:
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
#{
(true_or_false) \
? 'jackdaws love my giant sphinx of quartz' \
: 'Sphinx of black quartz, judge my vow.'
}
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Example #3:
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
#{(true_or_false) ? 'jackdaws love my giant sphinx of quartz' : 'Sphinx of black quartz, judge my vow.'}
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Example #4:
insert_string = (true_or_false) \
? 'jackdaws love my giant sphinx of quartz' \
: 'Sphinx of black quartz, judge my vow.'
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
${ insert_string }
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Personally I prefer #4 because it separates the logic from the definition of the string, helping to keep it easier to see what is going on, but your mileage might vary.
A heredoc is just like a string. So if your string allows variable evaluation and substitution (using no quotes or double quotes), you can do something like this:
switcheroo=true
mydoc = <<HDOC
This string will always appear
#{"This will only appear if switcheroo is true" if switcheroo}
HDOC
You have to put your conditions inside the brackets, standard in-string evaluation technique.
Please also note that this won't work if the quoting for your heredoc uses single quotes, meaning it's a literal string.