Login  Register

Is there a workaround for the read-only text method of the ListItem object?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Is there a workaround for the read-only text method of the ListItem object?

Jared
In regards to this PR that has yet to be released: https://github.com/asciidoctor/asciidoctor/pull/2034

I'm writing an extension that wraps our text with an angular filter `{{ ... | localize}}` so all our text in the manual goes through the same process we have for our app to get text translated. I've tried replacing the block with a new `ListItem` instance but then block gets rendered and if there are html elements (e.g., anchors) they are finally rendered as plain text.

Here's the code I'm working with and the input vs output vs desired output:

https://gist.github.com/jaredmcateer/5ff596493b4cf317e6799a5143a8076b

This is my first attempt at something like this with Asciidoctor and Ruby is not my forte so I'm not even sure if this is the best approach to achieving my goal.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator
Jared,

You're definitely on the right track, and a like where you are going with this!

Fortunately, there's a much simpler way to navigate the AST. The find_by method on AbstractBlock does a recursive descent to locate block nodes*, saving you a lot of trouble. It even accepts a block so you can operate on each node as it's discovered.

I updated the gist to show how this can be done:


Activate the extension when converting a document using:

 $ asciidoctor -r ./angular-localizer-treeprocessor.rb manual.adoc

To answer your question directly, while there will be a public API to set the raw text of a ListItem in 1.5.6, there's still no public API to read the raw text. But not to worry. Ruby makes it easy to reach into the object and work directly with the instance variable, which is what I used in the gist.

Here's the issue in core related to defining a public API to access this data.


I'm interested to know how that works out for you!

-Dan

* The one caveat at the moment is that it does not descend into table cells. If you study the mathematical extension, you can find a way to work around that limitation. See https://github.com/asciidoctor/asciidoctor-mathematical/blob/c0f8c4c9711235bafb10b8380e101f489d0e5679/lib/asciidoctor-mathematical/extension.rb#L41-L53

On Mon, Mar 6, 2017 at 12:04 PM, Jared [via Asciidoctor :: Discussion] <[hidden email]> wrote:
In regards to this PR that has yet to be released: https://github.com/asciidoctor/asciidoctor/pull/2034

I'm writing an extension that wraps our text with an angular filter `{{ ... | localize}}` so all our text in the manual goes through the same process we have for our app to get text translated. I've tried replacing the block with a new `ListItem` instance but then block gets rendered and if there are html elements (e.g., anchors) they are finally rendered as plain text.

Here's the code I'm working with and the input vs output vs desired output:

https://gist.github.com/jaredmcateer/5ff596493b4cf317e6799a5143a8076b

This is my first attempt at something like this with Asciidoctor and Ruby is not my forte so I'm not even sure if this is the best approach to achieving my goal.


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Is-there-a-workaround-for-the-read-only-text-method-of-the-ListItem-object-tp5335.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

Jared
Hello Dan,

Thank you very much for your help I was able to get much further with my work. The only thing I can't get wrapped now are titles. I can only seem to change the titles of the document and of admonitions that have no title defined (but I've suppressed that so there aren't `{{ |localize}}` titles all over the place.)

Here's what I've built upon the gist you provided:
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator
Nice work, Jared! You're well on your way.

Assigning a new title in 1.5.5 was problematic because the converted title was being cached. This is another fix that's coming in 1.5.6. Starting in that version, you'll be able to assign a new title using `title =`.


Using 1.5.5, you have to assign the subbed_title instance variable to nil as well. However, that instance variable has been removed in the upcoming 1.5.6.


Cheers,

-Dan

On Tue, Mar 7, 2017 at 10:38 AM, Jared [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hello Dan,

Thank you very much for your help I was able to get much further with my work. The only thing I can't get wrapped now are titles. I can only seem to change the titles of the document and of admonitions that have no title defined (but I've suppressed that so there aren't `{{ |localize}}` titles all over the place.)

Here's what I've built upon the gist you provided:
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Is-there-a-workaround-for-the-read-only-text-method-of-the-ListItem-object-tp5335p5340.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator
In reply to this post by Jared
Ah, good point about not collapsing endlines in paragraph content so post replacements (hard line breaks) still work. I forgot about that.

To make {{ and | localize }} appear on the same line as the text, you can use this instead:

lines = b.lines
lines[0] = %({{ #{lines[0]})
lines[-1] = %(#{lines[-1]} | localize }})

If there's only one line, both statements will operate on the same line.

-Dan

On Tue, Mar 7, 2017 at 2:31 PM, Dan Allen <[hidden email]> wrote:
Nice work, Jared! You're well on your way.

Assigning a new title in 1.5.5 was problematic because the converted title was being cached. This is another fix that's coming in 1.5.6. Starting in that version, you'll be able to assign a new title using `title =`.


Using 1.5.5, you have to assign the subbed_title instance variable to nil as well. However, that instance variable has been removed in the upcoming 1.5.6.


Cheers,

-Dan

On Tue, Mar 7, 2017 at 10:38 AM, Jared [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hello Dan,

Thank you very much for your help I was able to get much further with my work. The only thing I can't get wrapped now are titles. I can only seem to change the titles of the document and of admonitions that have no title defined (but I've suppressed that so there aren't `{{ |localize}}` titles all over the place.)

Here's what I've built upon the gist you provided:
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Is-there-a-workaround-for-the-read-only-text-method-of-the-ListItem-object-tp5335p5340.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator
In reply to this post by Jared
Instead of matching :paragraph, :admonition, the more accurate way is to check if the content_model:

if b.content_model == :simple
  # any type of paragraph, including a normal paragraph, admonition paragraph, etc
elsif b.context == :list_item
  # ListItem
end

Cheers,

-Dan


On Tue, Mar 7, 2017 at 2:34 PM, Dan Allen <[hidden email]> wrote:
Ah, good point about not collapsing endlines in paragraph content so post replacements (hard line breaks) still work. I forgot about that.

To make {{ and | localize }} appear on the same line as the text, you can use this instead:

lines = b.lines
lines[0] = %({{ #{lines[0]})
lines[-1] = %(#{lines[-1]} | localize }})

If there's only one line, both statements will operate on the same line.

-Dan

On Tue, Mar 7, 2017 at 2:31 PM, Dan Allen <[hidden email]> wrote:
Nice work, Jared! You're well on your way.

Assigning a new title in 1.5.5 was problematic because the converted title was being cached. This is another fix that's coming in 1.5.6. Starting in that version, you'll be able to assign a new title using `title =`.


Using 1.5.5, you have to assign the subbed_title instance variable to nil as well. However, that instance variable has been removed in the upcoming 1.5.6.


Cheers,

-Dan

On Tue, Mar 7, 2017 at 10:38 AM, Jared [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hello Dan,

Thank you very much for your help I was able to get much further with my work. The only thing I can't get wrapped now are titles. I can only seem to change the titles of the document and of admonitions that have no title defined (but I've suppressed that so there aren't `{{ |localize}}` titles all over the place.)

Here's what I've built upon the gist you provided:
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Is-there-a-workaround-for-the-read-only-text-method-of-the-ListItem-object-tp5335p5340.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator
In reply to this post by Jared

On Tue, Mar 7, 2017 at 2:39 PM, Dan Allen <[hidden email]> wrote:
Instead of matching :paragraph, :admonition, the more accurate way is to check if the content_model:

if b.content_model == :simple
  # any type of paragraph, including a normal paragraph, admonition paragraph, etc
elsif b.context == :list_item
  # ListItem
end

Cheers,

-Dan


On Tue, Mar 7, 2017 at 2:34 PM, Dan Allen <[hidden email]> wrote:
Ah, good point about not collapsing endlines in paragraph content so post replacements (hard line breaks) still work. I forgot about that.

To make {{ and | localize }} appear on the same line as the text, you can use this instead:

lines = b.lines
lines[0] = %({{ #{lines[0]})
lines[-1] = %(#{lines[-1]} | localize }})

If there's only one line, both statements will operate on the same line.

-Dan

On Tue, Mar 7, 2017 at 2:31 PM, Dan Allen <[hidden email]> wrote:
Nice work, Jared! You're well on your way.

Assigning a new title in 1.5.5 was problematic because the converted title was being cached. This is another fix that's coming in 1.5.6. Starting in that version, you'll be able to assign a new title using `title =`.


Using 1.5.5, you have to assign the subbed_title instance variable to nil as well. However, that instance variable has been removed in the upcoming 1.5.6.


Cheers,

-Dan

On Tue, Mar 7, 2017 at 10:38 AM, Jared [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hello Dan,

Thank you very much for your help I was able to get much further with my work. The only thing I can't get wrapped now are titles. I can only seem to change the titles of the document and of admonitions that have no title defined (but I've suppressed that so there aren't `{{ |localize}}` titles all over the place.)

Here's what I've built upon the gist you provided:
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Is-there-a-workaround-for-the-read-only-text-method-of-the-ListItem-object-tp5335p5340.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

Jared
Excellent! Thank you very much for your time and help. I think I've got this extension doing everything I need at this point.

Here's the final (at least for now) script in case anyone happens across this thread.
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator
Awesome!

If you'd like others to be able to find and use this extension, you might consider contributing it to the extensions lab.


Again, nice work!

-Dan

On Tue, Mar 7, 2017 at 3:39 PM, Jared [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Excellent! Thank you very much for your time and help. I think I've got this extension doing everything I need at this point.

Here's the final (at least for now) script in case anyone happens across this thread.
https://gist.github.com/jaredmcateer/9604e71e3eb3b39cd3285b67fb3578c3


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Is-there-a-workaround-for-the-read-only-text-method-of-the-ListItem-object-tp5335p5345.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

Jared
So I ran into a couple problems.  First, if there are double quotes in your text then it becomes invalid javascript syntax,  since angular treats: {{"He said " Hello " to me." | localize}} as $filter('localize')("He said " Hello " to me.") and that it is essentially a couple string literals surrounding a variable called Hello with no operators. An even bigger issue  is that Angular refuses to render bindings in the "{{}}" notation if they contain html and you must use the ng-bind-html="..."  attribute in the element of the parent.

I've worked around these issues while post processing of the final html using Cheerio.js to walk the DOM and convert all the bindings into ng-bind-html="..." but it's far from ideal, once I have more time I may revisit this and see if I can achieve a fix from within this processor script. For now I would say that this processor is not robust enough to be used as is.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is there a workaround for the read-only text method of the ListItem object?

mojavelinux
Administrator

If there are double quotes in your text then it becomes invalid javascript syntax,  since angular treats: {{"He said " Hello " to me." | localize}} as $filter('localize')("He said " Hello " to me.") and that it is essentially a couple string literals surrounding a variable called Hello with no operators.

That strikes me as a bug in Angular. Angular is the one that is adding the surrounding quotes, so it should be reponsible for escaping any quotes in the text.

You could escape the quotes when wrapping the text, though that runs the risk of breaking some of the inline AsciiDoc syntax. So an Asciidoctor postprocessor is the right solution there until Angular fixes the problem.
 
An even bigger issue  is that Angular refuses to render bindings in the "{{}}" notation if they contain html and you must use the ng-bind-html="..."  attribute in the element of the parent.

I'm not quite getting the picture of what the output needs to look like. The solution that is coming to my mind is customizing the template for the paragraph block, which is how you could add additional attributes to the HTML. But without knowing exactly what the HTML is supposed to look like, my proposal is incomplete. Can you show the HTML that is expected?

-Dan


--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux