Printing the raw contents of default CSS files with drupal_get_css - css

I have a custom Drupal 7 module that prints out the contents of a form, parses it and replaces all the form elements with equivalent divs, then prints out the HTML using DOMDocument().
This is then run through wkhtmltopdf to generate a PDF. I also have a little preg_replace that converts all occurrences of textarea/input to .div-textarea/.div-input in some inline CSS.
The issue, however, is that the default Drupal CSS files and the CSS files that are included in the custom theme are unable to be processed by this regex, because the drupal_get_css() function returns <link> tags and CSS's #import rule.
So the question is: do you know how I can get Drupal's theme output to print the actual raw contents of the CSS files in the HTML rather than these stylesheet links so that I can run the replacement function over it?
Or are there any better ways of copying CSS rules from one element/class to another?

I did this using a php funciton: file_get_contents
Then your code looks something like:
$css = file_get_contents(drupal_get_path('theme', 'my_theme') . 'css/my_css.css');
Then you can print that css in any template or append to any string to get your output.
Edit:
To get a list of all css files run: $css_list = drupal_add_css();

Once you have the CSS loaded as a string (see #danielson317's answer for a good example of how to do this) add to the head using drupal_add_html_head().
If you add the second key param, you can run your regex using hook_html_head_alter().

Related

Get selfrunning content from Tinymce 4.x

I would like to extract the HTML content created inside a tinyMCE editor to display it without any tinyMCE editor (displaying directly on a page).
I'm aware of tinyMCE's getcontent() function. But is there another function/parameter/plugin that I oversee with which you can extract it as selfrunning HTML code? There are many tinyMCE classes inside the content and it would be hard work to gather them or to convert them back into CSS styles.
So either an extraction with the tinyMCE specific classes would be great or any (automated) conversion from classes to CSS styles would be great. Or ist there any other way I oversee?
There is a format option that can be used to get pure HTML out of tinyMCE content:
tinymce.get('yourEditorId').getContent( {format : 'html'} );

PhpStorm forcing file type

I have my script.php file in project. It contains javascript, but I wanted it to ne created dynamicly, based on server-side data.
I setted it's Content-type to text/javascript with php, but PhpStorm does not see it as javascript file. So I loose javascript syntax hightlighting, error detection, live templates...
How can I force PhpStorm to see that .php file as javascript file?
You could make a naming Convention for this type of file.
Something like
foo.js.php
bar.js.php
baz.js.php
And then go to Options ALT + F7, navigate to File Types, and register *.js.php to javascript
What you are trying to do sounds like bad practise.
You maybe want to have something like this:
A real JS File which is compileable by Closure Compiler or something like that, which provides "bootstrap" functions, which take your values as parameter.
And if you write this in your template.php, everything is fine for PHP Storm:
<script>
doFoo(<?= $myFirstValue ?>, <?= $myFirstValue ?>);
</script>
Maybe you can work with data attributes in Your HTML which i think is cleaner then inline script tag.
Also take a look here: https://softwareengineering.stackexchange.com/a/126678/122683
Settings | Template Data Languages
Find your file there and assign JavaScript in right column to it (or whole folder --then it will be applied to all files in that folder and subfolders).
This will tell IDE to use JavaScript instead of default HTML as outer language for that file.
You can use the <<<JS heredoc syntax to make PhpStorm highlight a specific part of your file as Javascript. For example:
<?php
echo 'This is using PHP syntax highlighting';
echo <<<JS
<script>document.write('This is using JavaScript highlighting');</script>
JS;
You can also use file-specific language injection with ALT+ENTER, also see http://blog.jetbrains.com/phpstorm/2013/06/language-injection-in-phpstorm/ for further details on that.
You can also use the built-in Language Injection of IntelliJ (which is the platform on which phpStorm is built). For that, just mark the part of your file then press ALT + ENTER and use "Inject Language/Reference".

Parsing page data into sidebar - wordpress

What would be the proper procedure for accessing the current page html data and picking up all of a certain tag and throwing them into the sidebar as links?
I'm not sure your proficiency with php, but I'll give you and overview of what you'd probably want to do.
First, you need the HTML. I'm assuming you're running this on a page (in a page.php file or single.php file, or similar), this means that you have access to the global variable $post, which contains the html of the page in it. To access it you can use the helper function get_the_content(), this returns the html being displayed.
Next you need to parse through this to get the h2 tags. A simple regex can handle this, something like <h2[^>]*>(.*)</h2>. It's important to remember that this regex is very picky, so format your html correctly, no multiline h2s.
So now you have the html, and have parsed it with a regex to get the h2s. Now you need to generate the list from the results, and prepend it to the top of the content of the page. There are a ton of ways to do this, the easiest being just running the code in the right spot in the template file.
Of course there are probably better ways of doing this, I'd recommend you look at say a FAQ plugin (if that's what this is for), or do the lists manually (as this system can be broken), or possibly use a custom post type; but for your question, that's how I'd do it.

VIM: How to autocomplete in a CSS file with tag ids and class names declared in HTML file

I'm trying VIM for HTML, CSS and JavaScript editting. I've installed a number of plugins and configured VIM so that it allows me to autocoplete standart HTML/CSS code. And now I want to autocomplete my CSS code with tag ids and class names placed in HTML files just like in Apatana. For example:
I have 1.html file with the following lines:
<div id="my_id"> </div>
<div class="my_class"> </div>
And I have 1.css file with:
#my_id{border-style:dotted}
.my_class{border-style:solid}
When I'm edditing a CSS file and pressing <c-x><c-o> right after #, I want VIM to suggest me tag ids from the 1.html file. The same for tag class names. What should I do?
What I found helpful was adding the following line to my .vimrc:
autocmd FileType css,scss setlocal iskeyword+=-,?,!
which helps me autocompleting ids and classes with various special characters.
<C-x><C-o> is for language-specific keywords such as <table> in an HTML file or background-color in a CSS file, the right shortcuts for word completion are <C-n> and <C-p>.
Supposing your HTML and CSS files are loaded in two buffers (the HTML can be hidden or visible in a split), you are supposed to type a couple of characters of the id or class name and hit <C-n> for a list of possible completions.
Type :help ins-completion for more info.
I use AutoComplPop which provides realtime completion but there are other options.

How to disable header on frontpage of rst2pdf document?

I am generating some PDF's and I would like to disable the header on the frontpage. I know there are built-in templates in rst2pdf and one template is called coverPage but I don't seem to be able to get it to work.
The manual is saying you should use a
..raw:: pdf
PageBreak coverPage
statement but that will insert a empty before the coverpage, so how can I have a coverpage without a header and without using the oddeven directive (I want to use the same header on all remaining pages).
Thanks for your suggestions!
That's how you change the stylesheet after the cover page. You'll need to create a custom stylesheet that specifies what is the format of the first page and then change the style for the rest of document. Have a look at chapter 15 of the manual.
Note: current accepted answer contains broken link (linked website has gone).
The correct answer is simple:
1) In you style-file define:
pageSetup:
firstTemplate: coverPage
2) Then in your template, when you want to start using header/footer add:
..raw:: pdf
PageBreak cutePage
Make sure cutePage has set header/footer to true.

Resources