How to create a link to a header in restructuredtext? - restructuredtext

I have a document in restructuredtest like:
Header 1
========
and from some any other point (might be the same 'rst' file or a different one) I want to create a hyperlink to that header. So that when a user clicks on it, he gets to the page with the header Header 1
How to do that?
I tried to put the following line in the other document (according to this documentation):
see :ref:`Header 1`
but what I get is the following:
see Header 1
without any link...
I also tried to follow this documentation:
What I put in to the rst file is the following
see `Header 1`_
and what I see is the following link:
see `Header 1`_
which does not look very nice ...

Your first link was almost correct. You need to add a label preceding the section header, separated by a blank line. See Inline markup, Cross-referencing arbitrary locations, using the :ref: directive.
In your case:
.. _header-1-label-name:
Header 1
========
Some text
Here is a section reference: :ref:`header-1-label-name`.
Here is a section reference with a title: :ref:`Header 1 with a title <header-1-label-name>`.

In addition to the accepted answer, the label you add (in this case .. _header-1-label-name:) is required to have a dash. So a simple .. _label: won't do. Took me a while to figure that out.

Related

Get the text associated with a href element in a given page in scrapy

Currently my 'yield' in my scrapy spider looks as follows :
yield {
'hreflink':mylink,
'Parentlink':response.url
}
This returns me a dict
{
'hreflink':"https://www.southeasthealth.org/wp-content/uploads/Southeast-Health-Standard-Charges-2022.xlsx",
'Parentlink': "https://www.southeasthealth.org/financial-information-price-transparency/"
}
Now, I also want the 'text' that is associated with this particular hreflink, in that particular Parentlink. So my final output should look like
{
'hreflink':"https://www.southeasthealth.org/wp-content/uploads/Southeast-Health-Standard-Charges-2022.xlsx",
'Parentlink': "https://www.southeasthealth.org/financial-information-price-transparency/",
'Yourtext' : "Download Pricing Info"
}
What would be the simplest way to achieve that. I want to use Xpath expressions to get the "text" in a parentlink where href element = #href .
So far Here is what I tied -
Yourtext = response.xpath('//a[#href='+json.dumps(each)+']//text()').get()
but its not printing anything. I tried printing my response and it returns the right page - 'https://www.southeasthealth.org/financial-information-price-transparency/'
If I understand you correctly you want to get the text belonging to the link Download Pricing Info.
I suggest you try using:
response.xpath("//span[#class='fusion-button-text']//text()").get()
I found the answer to my question.
'//a[#href='+json.dumps(each)+']//text()'
This is the correct expression however the href link 'each' is case sensitive and it needs to match exactly for this Xpath to work.

how can I correct this restructured text syntax issue?

In the README.rst for blargs we see an syntactic error in the first sentence of the Quick Start section - specifically we see :class: rendered instead of only seeing Parser.
The actual code of that sentence is:
The preferred use of :class:`Parser` is via the ``with`` idiom, as follows:
How do we fix that syntax so that it correctly renders and what part of the restructured text docs informs us about this?

Dynamic Rstudio Code Snippet

I tend to use a lot of line breaks in my code like the following:
# Data =========================================================================
Where the entire comment is always 80 characters long (including the hashtag). What I would like to do is write a code snippet for Rstudio that will insert the hashtag, then a space, then allow the user to type out a series of words, then insert another space, and finally fill in a bunch of "=" until the 80 character limit is reached.
I'm not familiar with how snippets work at all so I'm not sure how difficult this is.
I have this much:
snippet lb
# ${1:name}
but I have no idea how to add a dynamic number of "=" signs. Also, lb = linebreak.
You can't do this with snippets, unfortunately; a snippet is a text template that contains fixed text with slots for user-inserted text.
There is a command built into RStudio to do something very similar, however; from the Code menu, choose Insert Section (or Ctrl+Shift+R). This will do exactly what you're describing, with two small differences:
The line will extend to 5 characters before the print margin (you can adjust the print margin in Tools -> Global Options -> Code.
The line is composed of - rather than = characters.
One advantage to sections marked in this way is that you can use them to fold and navigate inside the file (look at the editor status bar after adding one).
You can use the rstudioapi (which can return column position) inside the snippet to get something like what you want.
Below is a snippet I use called endhead. I use it by commenting my header title and then applying the snippet, eg:
# Section name endhead
which results in:
# Section name -----------------------------------------------------------------
snippet endhead
`r paste0(rep.int("-", 88 - rstudioapi::primary_selection(rstudioapi::getActiveDocumentContext())$range$start[2]), collapse = "")`
You can write a snippet to manipulate text (somewhat). I wrote the snippet below to do something similar to what you want to do. I'm still ironing out the issues (just asked this question).
snippet comm
`r paste0(
"#######################################><###################\n## ",
date(),
" -------------------------------\n## ",
eval(
paste0(
gsub(
".{1,51}\\s?\\K\\b",
"\n## ",
gsub("\\.", " ", paste0(text)),
perl = T
)
)
),
"###################################><###################\n"
)`
I think if you write an R code snippet using an anonymous function that accepts text input via $$, counts the nchar in the text, calculates the number of -'s needed at the end, and then uses eval(paste0()) to insert the comment you should be able to make it work. I'll post a comment or answer here if I figure it out. Please do the same on my question if you get it to work. Thanks. (P.S. Go Badgers!)
Inspired by nick's answer above I designed two snippets that allow the user to choose what level section to insert.
The first will fill-in the rest of the line with #, =, or -.
snippet end
`r strrep(ifelse(substr("$$", 1, 1) %in% c("-", "="), substr("$$", 1, 1), "#"), 84 - rstudioapi::primary_selection(rstudioapi::getActiveDocumentContext())$range$start[2])`
Just specify the character you want to use after end (will default to # if nothing or any other character is given). For example:
## Level 1 Header end<shift+tab>
## Level 2 Header end=<shift+tab>
## Level 3 Header end-<shift+tab>
end<shift+tab>
end=<shift+tab>
end-<shift+tab>
Produces the following lines:
## Level 1 Header ##############################################################
## Level 2 Header =============================================================
## Level 3 Header -------------------------------------------------------------
################################################################################
===============================================================================
-------------------------------------------------------------------------------
Similarly to what Josh was suggesting, the following snippet uses th $$ notation to pass the text following the snippet as described here.
snippet !
`r paste("##", substr("$$", 4, nchar("$$")), strrep(substr("$$", 2, 2), 79-nchar("$$")))`
Again this allows user to select the section level (#, =, or -). The first character after !# should be the header level character you want followed by a space and the header text. For example:
!## Level 1 Header<shift+tab>
!#= Level 2 Header<shift+tab>
!#- Level 3 Header<shift+tab>
Produces the following lines:
## Level 1 Header ##############################################################
## Level 2 Header ==============================================================
## Level 3 Header --------------------------------------------------------------
I prefer the end snippet above because it is more robust and only allows the characters #, =, or - to be inserted where as ! will allow anything, but it is shorter and, I think, easier to understand than calls to the rstudioapi.
!loon<shift+tab>
## n ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

Implementing syntax highlighting for markdown titles in PySide/PyQt

I am trying to implement a syntax highlighter for markdown for my project in PySide. The current code covers the basic, with bold, italic, code blocks, and some custom tags. Below is an extract of the relevant part of the current code.
What is blocking me right now is how to implement the highlighting for titles (underlined with ===, for the main title, or --- for sub-titles). The method that is used by Qt/PySide to highlight the text is highlightBlock, which processes only one line at a time.
class MySyntaxHighlighter(QtGui.QSyntaxHighlighter):
def highlightBlock(self, text):
# do something with this line of text
self.setCurrentBlockState(0)
startIndex = 0
if self.previousBlockState() != 1:
startIndex = self.blockStartExpression.indexIn(text)
while startIndex >= 0:
endIndex = self.blockEndExpression.indexIn(
text, startIndex)
...
There is a way to recover the previousBlockState, which is useful when a block has a defined start (for instance, the ~~~ syntax at the beginning of a code-block). Unfortunately, there is nothing that defines the start of a title, except for the underlining with === or --- that take place on the next line. All the examples I found only handle cases where there is a defined start of the expression, and so that the previousBlockState gives you an information (as in the example above).
The question is then: is there a way to recover the text of the next line, inside the highlightBlock? To perform a look-ahead, in some sense.
I though about recovering the document currently being worked on, and find the current block in the document, then find the next line and make the regular expression check on this. This would however break if there is a line in the document that has the exact same wording as the title. Plus, it would become quite slow to systematically do this for all lines in the document. Thanks in advance for any suggestion.
If self.currentBlock() gives you the block being highlighted, then:
self.currentBlock().next().text()
should give you the text of the following block.

How to represent markdown properly with escaping and line breaks?

I'm currently trying to build a chat app, using the official markdown package as well as underscore's escape function, and my template contains something like this:
<span class="message-content">
{{#markdown}}{{text}}{{/markdown}}
</span>
When I grab the text from the chat input box, I try to escape any HTML and then add in line breaks. safeText is then inserted into the database and displayed in the above template.
rawText = $("#chat-input-textbox").val();
safeText = _.escape(rawText).replace(/(?:\r\n|\r|\n)/g, '\n');
The normal stuff like headings, italics, and bold looks okay. However, there are two major problems:
Code escape issue - With the following input:
<script>alert("test")</script>
```
alert('hello');
```
This is _italics_!
Everything looks fine, except the alert('hello'); has become alert('hello'); instead. The <pre> blocks aren't rendering the escaped characters, which makes sense. But the problem is, the underscore JS escape function escapes everything.
SOLVED: Line break Issue - With the following input:
first
second
third
I get first second third being displayed with no line breaks. I understand this could be a markdown thing. Since I believe you need an empty line between paragraphs to get linebreaks in markdown. But having the above behaviour would be the most ideal, anyone know how to do this?
UPDATE Line break issue has been solved by adding an extra \n to my regex. So now I'm making sure that any line break will be represented with at least two \n characters (i.e. \n\n).
You should check the showdown docs and the wiki article they have on the topic.
The marked npm package, which is used by Telescope removes disallowed-tags. These include <script> of course. As the article I linked to above explains, there's still another problem with this:
<a href='javascript:alert("kidding! Im more the world domination kinda guy. Muhahahah")'>
click me for world peace!
</a>
Which isn't prevented by marked. I'd follow the advice of the author and use a HTML sanitation library. Like OWASP's ESAPI or Caja's html-sanitizer. Both of these project's seem outdated dough. I also found a showdown extension for it called showdown-xss-filter. So my advice is to write your own helper, and use showdown-xss-filter.

Resources