-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added multi_excerpt() #35939
Added multi_excerpt() #35939
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the pull request. I made a question inline.
# | ||
# multi_excerpt('This string is is a very long long long string ', 'string', radius: 5) | ||
# # => ...This string is i...long string ... | ||
def multi_excerpt(text, phrase, options = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between this and excerpt
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside a lengthy article (such as the one mentioned here), sometimes its useful to extract all occurrences of the phrase.
Example
article = <<~HEREDOC.delete("\n")
In your hands, my fellow citizens, more than in mine, will rest the
final success or failure of our course. Since this country was
founded, each generation of Americans has been summoned to give
testimony to its national loyalty. The graves of young Americans who
answered the call to service surround the globe. We observe today not
a victory of party, but a celebration of freedom — symbolizing an end,
as well as a beginning — signifying renewal, as well as change. For I
have sworn before you and Almighty God the same solemn oath our
forebears prescribed nearly a century and three quarters ago. The
world is very different now. For man holds in his mortal hands the
power to abolish all forms of human poverty and all forms of human
life. And yet the same revolutionary beliefs for which our forebears
fought are still at issue around the globe — the belief that the
rights of man come not from the generosity of the state, but from the
hand of God. Now the trumpet summons us again — not as a call to bear
arms, though arms we need; not as a call to battle, though embattled
we are — but a call to bear the burden of a long twilight struggle,
year in and year out, "rejoicing in hope, patient in tribulation" — a
struggle against the common enemies of man: tyranny, poverty, disease,
and war itself. Can we forge against these enemies a grand and global
alliance, North and South, East and West, that can assure a more
fruitful life for all mankind? Will you join in that historic effort?
In the long history of the world, only a few generations have been
granted the role of defending freedom in its hour of maximum danger. I
do not shrink from this responsibility — I welcome it. I do not
believe that any of us would exchange places with any other people or
any other generation. And so, my fellow Americans: ask not what your
country can do for you - ask what you can do for your country.
HEREDOC
(byebug) excerpt(article, "country", radius: 60)
"...rest the final success or failure of our course. Since this country was founded, each generation of Americans has been summoned..."
(byebug) multi_excerpt(article, "country", radius: 60)
"...rest the final success or failure of our course. Since this country was founded, each generation of Americans has been summoned...generation. And so, my fellow Americans: ask not what your country can do for you — ask what you can do for your country...."
Related SO question here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would adding it as an option to excerpt
be more elegant? excerpt(article, "country", radius: 60, multi: true)
or excerpt(article, "country", radius: 60, multi: 2)
to get the first two etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thought did cross my mind, the amount of coding (time) required for that was beyond what was available to me during that week
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like @robzolkos suggestion. Can you update the PR?
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Summary
Some time ago I needed to summarize an article/blog in a landing page for SEO purposes,
and the combination of excerpt() and hightlight() wasn't enough for the job.
So I had an attempt at implementing a
multi_excerpt()
to complementthe pre existing
excerpt()
method.