How should I write the Asciidoc(tor) to have the following output:
* item 1
* item a
paragraph 1 inside item a
* item a.1 inside paragraph 1 inside item a
paragraph a.1 inside item a.1
* item a.2 inside paragraph 1 inside item a
paragraph a.2 inside item a.1
paragraph 2 inside item a
* item b
paragraph 1 inside item b
* item 2
UPDATE: #TigerTV.ru suggested a nice trick, but it requires the capability to hide the bullets in bullet lists. How can this be done?
Thanks
https://asciidoctor.org/docs/user-manual/#attaching-to-an-ancestor-list:
You may find that you need to attach block content to a parent list item instead of the current one. In other words, you want to attach the block content to the parent list item so it becomes a sibling of the child list. To do this, you add a blank line before the list continuation.
* parent list item
** child list item
+
paragraph attached to parent list item
More complex example:
* Foo
** Aaa
** Bbb
+
Continuation of Foo
* Bar
+
Continuation of Bar
* Baz
+
Continuation of Baz
** Ccc
** Ddd
* Xyz
Notes:
You need blank line after Bbb. This is by design:
The blank line signals to the list continuation to move out of the current list so it attaches the block to the last item of the parent list.
You also need blank line before Ccc. This is currently an open issue #2509.
A block attached to the grandparent list item:
* grandparent list item
** parent list item
*** child list item
+
paragraph attached to grandparent list item
Using blank lines to back out of the nesting may feel fragile. A more robust way to accomplish the same thing is to enclose the nested lists in an open block. That way, it’s clear where the nested list ends and the enclosing list continues:
* grandparent list item
+
--
** parent list item
*** child list item
--
+
paragraph attached to grandparent list item
Think this will work:
* item 1
** item a +
paragraph 1 inside item a
+
--
*** item a.1 inside paragraph 1 inside item a +
paragraph a.1 inside item a.1
*** item a.2 inside paragraph 1 inside item a +
paragraph a.2 inside item a.1
--
+
paragraph 2 inside item a
** item b +
paragraph 1 inside item b
* item 2
Tested with Asciidoctor 1.5.6.1.
You can use several asterisks for levels:
* item 1
** item a
*** paragraph 1 inside item a
**** item a.1 inside paragraph 1 inside item a
**** item a.2 inside paragraph 1 inside item a
*** paragraph 2 inside item a
** item b
*** paragraph 1 inside item b
* item 2
https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#lists
Related
Say I have an app that's trying to relate a string with an int. There are many strings and I want to keep a list of the top N that have occurred.
For example say the strings looked like this:
item 0 = "Foo"
item 1 = "Foo"
item 2 = "Boo"
item 3 = "Boo"
item 4 = "Bar"
item 5 = "Sar"
Say my cache has a cap of 3. Here's how I want it to behave:
item 0 = TryGet "Foo" - Add. "Foo" occurrences = 1
item 1 = TryGet "Foo" - return. "Foo" occurrences = 2
item 2 = TryGet "Boo" - Add. "Boo" occurrences = 1
item 3 = TryGet "Boo" - return. "Boo" occurrences = 2
item 4 = TryGet "Bar" - Add. "Bar" occurrences = 1
item 5 = TryGet "Sar" - At capacity. Remove elem with lowest occurrences, "Bar". Add "Sar"
So each current cache item gets a weight and on discovery of a new item when at capacity, we toss out the element with the lowest number of get occurrences. Is there a name for this kind of caching algorithm?
EDIT:
I was looking for Least-Frequently Used
https://en.wikipedia.org/wiki/Cache_replacement_policies#Least-Frequently_Used_.28LFU.29
I was looking for Least-Frequently Used
Having the following string:
>db|version.x|name of entry 1
properties of entry1
>db|version.x|name of entry 2
properties of entry2
In the lines starting with ">", I would like to move the number to the front, separated by a space from the rest of the old text, leaving properties of the entry intact, so that it looks like this:
>1 db|version.x|name of entry 1
properties of entry1
>2 db|version.x|name of entry 2
properties of entry
awk '{sub(/^>/, ">"$NF" ")}1' File
>1 db|version.x|name of entry 1
properties of entry1
>2 db|version.x|name of entry 2
properties of entry2
Based on the following data in a YAML file, is it possible to create a regular expression in Ruby which matches the respective Group and Item keys from a list
Source Data
Groups:
GroupA:
- item 1
- item 3
Group B:
- itemA
- item 3
C:
- 1
- item 3
Test String:
GroupA item 1
Group B itemA
c item 1
C 1
GroupA 1
Expected Match Groups
Match 1:
1. GroupA
2. item 1
Match 2:
1. Group B
2. itemA
Match 3:
1. C
2. 1
Thanks for any help!
Ian
==================================
Update: Following Tin Mans comment -
Here's some further background...
A class inside a plugin exists which contains a number of methods. Each method receives a string which is parsed to determine what action is performed. In some methods, the contents of the string are used in the subsequent actions - when this is required a regular expression is used to extract (or match) the relevant parts of the string. Unfortunately there is no control over the upstream code to alter this process.
In this case, the string is in the form "Group Item Status". However the group and item names are not necessarily single words and each group does not have to contain all items. e.g.
"Group A Item 1"
"c item 1"
"GroupA 1"
So, what's needed is a method of parsing the input string to get the respective Group and Item so that the correct values are passed to methods further down the line. Given that other comparable methods in the class use regular expressions, and there is a YAML file which contains the definitive list of group - item pairs, a regular expression was my first line of thought.
However, I am open to better approaches
Many thanks
Ian
Why would you want to match anything in a YAML file? Load it into Ruby using the YAML parser, and search it, or modify in memory.
If you want to save the modified file, the YAML parser can emit a Ruby object as YAML, which you then save.
require 'yaml'
yaml = '
---
Groups:
GroupA:
- item 1
- item 3
Group B:
- itemA
- item 3
C:
- 1
- item 3
'
yaml = YAML.load(yaml)
# => {"Groups"=>{"GroupA"=>["item 1", "item 3"], "Group B"=>["itemA", "item 3"], "C"=>[1, "item 3"]}}
yaml['Groups']['GroupA'].first
# => "item 1"
yaml['Groups']['Group B'][1]
# => "item 3"
yaml['Groups']['C'].last
# => "item 3"
Based on the above definitions, manipulating the data could be done like this:
yaml = YAML.load(yaml)
groups = yaml['Groups']
new_group = {
'groupa_first' => groups['GroupA'].first,
'groupb_second' => groups['Group B'][1],
'groupc_last' => groups['C'].last
}
yaml['New Group'] = new_group
puts yaml.to_yaml
Which outputs:
---
Groups:
GroupA:
- item 1
- item 3
Group B:
- itemA
- item 3
C:
- 1
- item 3
New Group:
groupa_first: item 1
groupb_second: item 3
groupc_last: item 3
There's a reason we have YAML parsers for all the different languages; They make it easy to load and use the data. Take advantage of that tool, and use Ruby to modify the data, and, if needed, write it out again. It would be one huge YAML file before I'd even think of trying to modify it on disk considering it's so easy to do in memory.
Now, the question becomes, how do you search the keys of a hash using a regex?
yaml['Groups'].select{ |k,v| k[/^Group/] }
# => {"GroupA"=>["item 1", "item 3"], "Group B"=>["itemA", "item 3"]}
Once you have the ones you want, you can easily modify their contents, substitute them back into the in-memory hash, and write it out.
How can i replace DokuWiki nested list string with using one or two regexps in Ruby?
For example, if we have this string:
* one
* two
* three
* four
we should get this HTML:
one
two
three
four
I've made a regexp replacing the whole list. E.g.:
s.sub!(/(^\s+\*\s.+$)+/m, '<ul>\1</ul>')
And it works as it should. But how to replace the single list items?
The regex :
Here are some example lists :
* first item
* second item
No longer a list
* third item? no, it's the first item of the second list
* first item
* second item with linebreak\\ second line
* third item with code: <code>
some code
comes here
</code>
* fourth item
The regex for matching all lists
(?<=^|\n)(?: {2,}\*([^\n]*?<code>.*?</code>[^\n]*|[^\n]*)\n?)+
View it in action : http://rubular.com/r/VMjwbyhJTm
The code :
Surround all lists with a <ul>...</ul>
s.sub!(/(?<=^|\n)(?: {2,}\*(?:[^\n]*?<code>.*?<\/code>[^\n]*|[^\n]*)\n?)+/m, '<ul>\0</ul>')
Add missing <li>s (s2 in the following code is the string with <ul>...</ul> added)
s2.sub!(/ {2,}\*([^\n]*?<code>.*?<\/code>[^\n]*|[^\n]*)\n?/m, '<li>\1</li>')
Note :
Nested lists can not be handled with this regex. If this is a requirement, a parser will be more adapted !
Say I have a text file like so:
apples
pencils
juice
apricots
John Doe
the string system is amazing
I want a list created for every 3 items in the text file like so (as strings of course):
item list 1={apples, pencils, juice}
item list 2={apricots, John Doe, the string system is amazing}
However, the text file will contain more than 6 lines and different text of the lines so it cannot be specific like:
set line1 of myfile to item 1 of mylist
set line2 of myfile to item 2 of mylist
set line3 of myfile to item 3 of mylist
set line4 of myfile to item 1 of mySecondlist
set line5 of myfile to item 2 of mySecondlist
set line6 of myfile to item 3 of mySecondlist
Try this. The code should be self explanatory, but if you need help ask questions.
-- get the text from the file
set theFile to choose file
set theText to read theFile
-- turn the text into a list so we can loop over it
set theTextList to paragraphs of theText
-- put every 3 items into a list and add it to the finalList variable
set listCount to count of theTextList
set finalList to {}
repeat with i from 1 to listCount by 3
if (i + 2) is not greater than listCount then
set end of finalList to items i thru (i + 2) of theTextList
else
set end of finalList to items i thru listCount of theTextList
end if
end repeat
-- show the list of lists result
return finalList
If your file contained the values you showed, here's the result...
{{"apples", "pencils", "juice"}, {"apricots", "John Doe", "the string system is amazing"}}