Advertisement
Guest User

insert.d - BaussProjects - daweb

a guest
Dec 4th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.97 KB | None | 0 0
  1. // http://baussprojects.comlu.com/projects.php#daweb
  2. /*
  3. *    Inserts an attribute to a html tag with a specific id.
  4. */
  5. string insertAttribute(string html, string id, string attributeName, string attributeValue) {
  6.     import std.algorithm : countUntil;
  7.     import std.string : format;
  8.    
  9.     ptrdiff_t index = countUntil(html, format("id=\"%s\"", id));
  10.     index += countUntil(html[index .. $], ">");
  11.     string beforeStr = html[0 .. index];
  12.     string n = beforeStr ~ " " ~ format("%s=\"%s\"", attributeName, attributeValue);
  13.     return n ~ html[index .. $];
  14. }
  15.  
  16. /**
  17. *    Inserts html code into a specific html tag with a specific id.
  18. */
  19. string insertHtml(string html, string id, string insertHtml) {
  20.     import std.algorithm : countUntil;
  21.     import std.string : format;
  22.    
  23.     ptrdiff_t index = countUntil(html, format("id=\"%s\"", id));
  24.     index += countUntil(html[index .. $], ">");
  25.     string beforeStr = html[0 .. index + 1];
  26.     string n = beforeStr ~ insertHtml ~ html[index + 1 .. $];
  27.     return n;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement