Link to a line in a HTML? - html

Is it possible to link to a line number in a html file? I want to link someone to part of a very large document which is on a webpage but the whole thing is in one tag so is not split into sections with IDs I can link to.
Here is the page I'm talking to, id like to link to "LEVEL 46" can I do this?
I would be just as happy with another workaround such as searching for the text or anything, I assume this could be made more complicated by it being in various wrappers, assuming it was just a plain .txt file could you do it?
Edit: im not writing the web page im trying to link to a part of someone elses, so i cannot add IDs etc

No it doesn't appear to be possible.

<a href="#a-place-in-the-document></a>
...
<h1 id="a-place-in-the-document">There's a link to here!</h1>
The link will have the page jump to the element with the specified ID.
For example: http://example.com#hello will link to http://example.com and immediately go to the element with ID of hello.
In the case where you can't have IDs (such as in text files on gamefaqs), you'd need to provide a search string, for people to quickly search and find whatever section you need (such as [LV46]), and have your readers to search for it.

You would need to convert all of your Headings into anchor tags.
So for your example the link would be:
<a href="#Level46">
The heading itself would be:
<h1 id="Level46">Level 46</h1>
Hope this helps.

If it is just a text file, or a collection of text files in a directory structure, you can create a parallel directory structure so people can browse
http://pdssbn.astro.umd.edu/holdings/
as
http://pdssbn.astro.umd.edu/byline/holdings/
and generate line numbers and/or anchors to individual lines on the fly, even if other non-text files are under the /holdings/ tree, with an Apache HTTPd AliasMatch like this
AliasMatch /byline/holdings/.*[.](asc|cat|lbl|tab|txt) /path/cgi-bin/pds_byline.cgi
and a symlink e.g. assuming your tree is under top/ e.g. top/holdings/,
ln -s . .../top/byline
and a simple script (cgi-bin/pds_byline.cgi above) that converts the text file to HTML on the fly. I have created a Git repo to do just that here; that is configured for Planetary Data System (PDS) data sets under http://pdssbn.astro.umd.edu/holdings/.
You would of course need access to the Apache HTTPd configuration files (/etc/httpd/conf/conf//.conf) to do this, to put in a
<Directory .../top/cgi-bin>
AllowOverride All
</Directory>
entry to use a .htaccess file in cgi-bin/, and the AliasMatch above, at a minimum. N.B. AliasMatch cannot go into the .htaccess file.
Caveat: this only creates anchors by line number; if the file changes over time then existing links to those line numbers will be broken; you could of course do the same thing instead looking for specific text strings like "Level 46" and inserting the relevant anchors on the fly.

<a name="destination" id="destination"></a>Destination anchors
Destination anchors
Source:
http://www.motive.co.nz/glossary/anchor.php#destination

Maybe you could define an anchor for the specific line as, for example, a section element. Then you could link to that anchor.
<section name"sec1">
<a href="sec1">

Related

How do you pull a value from a URL tag and modify it in HTML?

I'm in marketing so I use URL tags to pass specific information to my pre-sale pages.
I'm looking to write the HTML so that it will read the the tag (for example image=1_1) remove the last 2 digits (so we are left with 1 in this example) and then add .jpg to it (1.jpg)so it loads that image from a specific folder.
How would I got about doing this?
My bad. I should have work my answer according to your approach.

Displaying a .txt file as pure text without editing the file contents

I have a .txt file containing code that I cannot change the contents of. And I need to display it in two ways.
One way is inside a div as selectable, copy-able type (currently done with:
<pre><?php include '/file_location.txt';?></pre> ).
The other way is as a direct link to the .txt file so such link can have it's address copied and emailed to someone, saved as..., or any other function one might like a direct link for. (So just like <a href="/file_location.txt"> basically.)
The issue is that when php including the text file into a div any <%> strings interfere with the original source text. I need to preserve the integrity of the original .txt files (so I can't go changing all the left carrots into <).
So is there a good way to display the contents of the text file without issues with < > and still maintain it's original integrity for sake of direct-linking?
EDIT:
I currently have two separate files performing this function, one with html encodings and the raw unedited .txt file. I'd really like to get these two displays working with just one file so that each new bit of source code doesn't need to be converted to an html-friendly version and adding just its .txt file will grant both view options.
EDIT 2:
Using <textarea> instead of <pre> will not interfere with the < characters and i could CSS it to look how I want, but I don't like the idea of the user being able to resize it themselves.
You can use
<?php echo htmlspecialchars(file_get_contents("file.txt")) ?>
instead of
<?php include '/file_location.txt';?>
to display special HTML characters from a text file.
I am using this for my php files. I think it will be usefull for you too.
<?php
highlight_file("test.php");
?>
edit: I tried on a html file and it worked.
I would try to add <pre></pre> at the beginning/end of your .txt. I'm not sure if I fully understand your question, but I think this will not interfere with the <> tags.

How to go to sub-directory with relative URI

Sorry if it so basic but I could not find the answer by searching.
If we are in the page http://www.example.com/a-dir-without-trailing-slash how we can reach the sub-directory http://www.example.com/a-dir-without-trailing-slash/pic using relative URI? (we do not know the current directory name(i.e. a-dir-without-trailing-slash)
Some more explanation:
a-dir-without-trailing-slash is the name of an article in the website. It is not an actual directory nor an actual file name. Now, I want to get the pictures that are used in this article by addresses like:
http://www.example.com/a-dir-without-trailing-slash/pic/1
http://www.example.com/a-dir-without-trailing-slash/pic/2
,...
and in the webpage html, I would refer to them with something similar to:
<img src="pic/1" />
If the original article address was in the form of http://www.example.com/a-dir-with-trailing-slash/, the above example would work finely. I want to know if is it possible to get a relative URI with current article addresses (without trailing slash)?
Thank you very much
I suppose you want to avoid hard coding "slugs" in the content so that they can be stored and manipulated independent of each other.
One solution is to use the base tag which allows you to specify the prefix that is added to relative URLs instead of typing them all over the place.
Make sure that your website uses absolute URLs where necessary.
Modify your CMS to "generate" and place the following tag inside the head section that contains trailing slash:
<base href="/a-dir-without-trailing-slash/">
Then you can use relative URLs inside the content, for example:
<img src="pic/1">
<!-- http://www.example.com/a-dir-without-trailing-slash.html/pic/1 -->
You need server side scripting to add the filename to urls (or may be just one '> tag in the head). – Salman
Bounty get.

Regex: img src urls that don't have multiple paths

Going through another crazy website migration!
I have HTML img src urls that look like this
http://blog.example.com/imagename.jpg
Image formats can also be jpg, png, or gif
We need a regex that finds every url that has the domain then "/imagename.jpg" immediately after.
Very new to regex, what would the expression be?
Better Alternative for WordPress Migrations
If you are moving your website and you will want to replace all references to the old site with the new domain, I suggest you use David Coveney's Serialized Search & Replace DB v2.1.0. You'll want to run this on a new copy of the database, always have a backup handy. Import the database on the destination server, then run the tool - You don't even have to upload the server files.
When I do this coming from a development server to live domain, I usually do two search & replaces:
One for URLs, very basic:
Search: mywebsite.devserver.com
Replace: my-new-website.com
And one for file paths:
Search: /vhosts/devserver.com/mywebsite
Replace: /vhosts/my-new-website.com/httpdocs
(Note: This is assuming the majority of the file path is the same for both servers. Your search & replace paths may need to be more accurate)
The reason you want serialized search and replace is that some data is stored in PHP-serialized format, and if you change the value with a text editor or in MySQL directly, it may not be able to unserialize afterwards.
Regex Answer
Select images hosted by blog.example.com with the following regex pattern:
((http|https)://blog\.example.com/[^ \r\n]+\.(jpg|jpeg|png|gif))
Which basically searches for this: http(s)://blog.example.com/*.(jpg/png/etc)
Matches the URLs in the following examples:
http://example.com/imagename.jpg
http://blog.example.com/imagename.jpg
http://blog.example.com/favicon.png
http://blog.example.com/uploads/2013/05/kitten.gif
https://blog.example.com/ssl-secure.png
This is my favorite gif https://blog.example.com/some-hilarious-image.gif hahaha
DOES NOT match any of these:
blog.example.com/google.png
https://blog.google.com/google.png
our website is http://blog.example.com and has an image named /imagename.png
http://blog.example.com/
WHY it doesn't match those (by line):
Does not include http(s)://
Hosted by google
Paragraph text, where the URL is split into two parts
Not an image
$1 returns the full URL of the image.
I tested this on RegexTester.com. You can copy the pattern in the top field, and all of the examples in the box below. The red highlights are matches.
Many good suggestions already, and why would a wordpress site hardcode domain name to links, but thats not our problem right now. If you need a regex then try this:
(?<=<img).+(?<=src=["'])(.+(?:jpe?g|gif|png))
EXPLAINED:
(?<=<img).+(?<=src=["']) - be sure we're inside an <img> tag up to src attribute
(.+(?:jpe?g|gif|png)) capture everything up to required extension

Headings created inside of a template

I have a number of templates that create headings based on a formula. I am wondering if there is anyway to create an "edit" link that will take you directly to that section? The way that it currently works, the edit link takes you to editing the template itself. Could I possibly create a customized link that would keep you on the page and take you to right part?
Here is some sample code to help clear things up...
Template:Head:
==={{{1}}}===
This is a heading titled "{{{1}}}"
Test Page:
=Section 1=
{{head|1.1}}
{{head|1.2}}
{{head|1.3}}
=Section 2=
{{head|2.1}}
{{head|2.2}}
{{head|2.3}}
At the moment, if I want to edit the information for template "2.3", I have to edit all of section 2. (Note that for this example, that isn't a big deal. For the actual templates I am working with on my site, the templates have dozens of parameters and there are sometimes 10 or more in a section.)
Bottom line, is there way to create a custom edit link inside of the {{head}} template that would take you directly to editing the templates call on the page "Test Page"? Hope that makes sense.
Edit: Is there perhaps a way to make use of "anchor" tags? Can anchors be passed in to the URL?
To restate your problem, when you transclude a section heading the header isn't treated as being part of the destination page, so the edit link takes you back to the source. So you need a separate container for the template in order to edit it individually, and a complete section is the smallest editable container.
The only way I can think of doing this is using subpages (or virtual subpages if you don't have that ennabled in this namespace, doesn't change anything). So instead of placing {{head|1.1}} on MyPage, put it on MyPage/Subpage1 and then transclude that into MyPage in the usual way ({{:MyPage/Subpage1}}).
{{head}} can then include a custom edit link to the template input by using HTML heading tags (<h2> is equal to ==, etc.) to suppress the standard edit link and then use one of these templates (probably {{ed right}}) to create a custom edit link pointing to MyPage/Subpage1.
The way to create anchors in Mediawiki, by the way, is to use a <span id="name"/> tag, but that doesn't create a container that can be edited (or at least, not that I've been able to work out through URL tinkering).
I'm pretty sure there's no way to do that. As far as MediaWiki's section editing feature is concerned, the only thing that begins a new section is a line of the form:
=== Some text here ===
with the number of = signs determining the level of the heading. There's no way to get MediaWiki to let you edit any segment of the document that doesn't begin and end with such a line (or the beginning or end of the page).
Well, OK, I'm sure you technically could do it with an extension, in the sense that you can do anything with a MediaWiki extension. All you'd need to do is provide some way (e.g. a special parameter in an edit URL) for to user to indicate "I want to edit this template", then extract the template from the wikitext, present it to the user for editing, and write the result back into the page text over the original.
The tricky part will be extracting the template from the page source. (Finding and replacing templates on a page is a fairly common task for MediaWiki bot writers, so you might want to look for ideas there.) Whatever method you end up using for that, there will probably be edge cases where you need to give up and tell the user "Sorry, but I can't figure out how that template is transcluded here."