Extract all CSS rules set that have a certain rule - php

I was asked to change the font used in my client's website. they are using a commercial template.
I searched in the stylesheet files and saw that there are almost 400 occurrences spread in 12 different files for "font-family", I need to override them all somehow.
I don't want to use the universal rule (*) since it feels like it can cause unexpected results (like preventing me to use a second font-family)
Any Ideas?
I thought maybe I can use some tool/software to extract the entire rule in each of files into a separate file(s) and than I could easily copy them and search-replace the font family, is there such a tool/software?
I could also use PHP to look in those files and search/replace on run time but it seems like bad practice to run this on every page load.
Please advise, what would you do?
** MORE INFO: **
I don't want to edit the original files or copy them entirely. I want to extract just the rule name and the font-family attribute and value.
Example:
.some_rule {color: red;font-size: 16px;**font-family: 'whatever';**float: right;}
To become / To extract:
.some_rule {**font-family: 'whatever';**}
Then I can copy only this part to my override.css file (for example).
Thanks!
** UPDATE **
I'm sure there is a nicer way to do it, but for my needs, just to get things done in a few lines of code, this is what I did:
copied the files to temp directory
formatted them so EVERYTHING will be in a new line (declaration, attr + value, closing curly brace);
run the php code I wrote - http://codepad.org/D2YOKYJ5
This snippet creates a new file (or write to an existing one) with only what I needed.

Sounds like a replace action that a simple editor could do in a matter of minutes.
But you might consider migrating to SCSS or another CSS precompiler.
The advantage of SCSS (over SASS, for instance), is that it's completely CSS compatible, so you can just rename all CSS files to SCSS, configure an SCSS compiler, and you're up and running.
Once you've got that, you can start replacing the font family with variables or mixins, and gradually you can add more and more structure to the CSS files by refactoring certain parts of it.
If you don't want to modify the CSS at all (since it is a commercial template, and you may want to be able to download updates), you can collect all selectors for the font and make a new CSS file with them, overriding just the fonts for those specific selectors.

Related

Symfony - Insert values programatically into CSS files

Ok, here's the scenario. We have a Symfony2 project and the same core code is shared by two websites, the 2nd website simply has its own App folder, with the src and vendor folders symlinked to the 'master' install. This works great and with the specific parameters in the 2nd app folder, allows us to run with a different database and thus offer the same technical solution to two companies. hurrah :)
BUT. We obviously want them styled differently via CSS, the values for which need to come out of the database. I realise we could simply code them, but with future projects in mind, we want them to be in the database and thus easy to update centrally.
So, the problem is how can we insert these database sourced values into the CSS file? I've looked into SASS etc, which would be a step in the right direction, but the values are still defined within the CSS file, not outside it.
We could do a dynamic CSS file, powered by Twig, but that's frankly using a sledgehammer to crack a nut.
Has anyone come across this problem and found a suitable answer?
I did never try it but i think that you can render a css file with Twig like you render any other file. You only have to create a route, a controller and a view.
Sorry did not read last rules..
You could add a datetime stamp in the css mysql-table. Is it changed? Render and save a new version of your css file. Otherwise skip it.
On the other hand Twig uses caching as much as posible so what is the problem?
You could create a service that write a css file in your css folder.
Call this services when you make any changes in the DB. (You can make a listener for that).
You could also use asseticBundle to manage all your css files.

SASS and/or LESS - can I create dynamic CSS files on the fly?

I'm using CakePHP to build my site (if that matters). I have a TON of elements/modules each having their own file and fairly complicated CSS (in some cases).
Currently the CSS is in a massive single CSS file, but for sanity sake (and the below mentioned details), I would like to be able to keep the CSS in it's own respective file - ie css/modules/rotator.css. But with normal CSS, that would call a TON of CSS files.
So, I started looking into SASS or LESS per recommendation. But - it seems these are supposed to be compiled then uploaded. But in my case, each page is editable via the CMS, so a page might have 10 modules one minute, then after a CMS change it could have 20 or 5...etc. And I don't want to have to compile the CSS for every module if it's not going to use it.
Is there a way I can have a ton of CSS files that all compile on the fly?
Side note: I'd also like to allow the user to edit their own CSS for a page and/or module, which would then load after the default CSSs. Is this possible with SASS and/or LESS?
I don't need a complete walkthrough (though that would be awesome), but so far my searches have returned either things that are over my head related to Ruby on Rails (never used) or generic tutorials on each respective CSS language.
Any other recommendations welcome. I'm a complete SASS/LESS noob.
Clarified question:
How do I dynamically (server-side) combine multiple CSS files using LESS? (even a link to a resource that would get me on the right track is plenty!)
If you want to reduce the number of CSS files & you have one huge css file that has all the component css, just link to it on all pages & make sure you set cache headers properly.
They will load the file once and use it everywhere. The one pitfall is initial pageload time; if that's not an issue go with this solution. If it is an issue consider breaking down your compiled CSS files to a few main chunks (default.css, authoring.css, components.css eg.).
Don't bother trying to make a custom css for each collection of components, you will actually be shooting yourself in the foot by forcing users to re-download the same CSS reorganized in different ways.
Check out lessphp (http://leafo.net/lessphp/). It's a php implementation of less and can recompile changed files by comparing the timestamp.
Assuming that 'on the fly' means 'on pageload', that would likely be even slower than sending multiple files. What I would recommend is recompiling the stylesheets whenever a module is saved.
The issue of requiring only necessary modules should be solved by means of CMS. It has nothing to do with SASS or LESS.
If your CMS is aware of which modules current page has, do not run a SASS/LESS compilation (it will be painfully slow unless you implement caching which is not a trivial task). Instead, adjust your CMS's logic so that it includes each module's CSS file.
Advanced CMSs like Drupal not only automatically fetch only necessary CSS files, but also assemble them into a single file and compress it.
And if your CSS is not aware of which modules current page has (e. g. "modules" are simply HTML code that is saved into post body), then you can't really do anything.
UPD: As sequoia mcdowell says in his answer, making users download one large CSS file once is better than making them download a number of lesser CSS files that contain duplicate code. The cumulative size of all those smaller CSS files will turn out to be larger than the size of a full CSS file.

editing css files through php

my head was spinning over this idea for a long time . and i though this is the right plce to ask for help ..
so what i want to do is make a really simple php file that make a search and replace in css files but the problem that i have is that a single css file can (as you all know ) have a 100 or more background tags and all i want to edit is one of them .. and what is some body added a line before or after ... so search and replace for the whole section of the css file that i want to replace well be useless unless it is all the same ..
i really have a bad time explanning but i hope that you get the main idea ..
all i want is some guidelines in this .. help is really appreciated/
I wouldn't suggest directly editing a linked CSS file through PHP (file_get_contents()) or something, but rather conditionally link different CSS files. Or perhaps, conditionally change what you need to change through CSS directly in HTML with PHP. For example, need to change background (for some reason) for different users, you would conditionally change the image source through PHP.
Why exactly do you want to do this? Maybe a CSS alternative like LESS is a viable option (it has support for variables etc)
You can either use php to load a stylesheet.php that is dynamically generated or use javascript to "compile" it on the client side (like LessCss)

loading css or javascript from non-public directory

is there a way to load css and/or javascript files from outside of the public web directory?
for example on my hosting service i have /public_html but don't want these files to exist in the public directory and want them in a directory outside of the public directory in a sibling directory /system (i am using codeigniter) within the /system/application/view/
Ultimately, Javascript and Stylesheets are processed on the client side. For that reason, there is no solution that would truly hide your javascript or CSS from the public.
One possible solution is to load the required CSS/ Javascript file via PHP using something like file_get_contents() and then outputting that directly to the page using inline styles / scripts.
This doesn't really solve your problem of hiding the code / styles from the public though. It would give you the option of filtering all code and styles through some kind of packer or obfuscatory, although there's no reason you couldn't do that with your static files (and at much less of a processing expense)
Yes -- in a way -- and Minify [http://code.google.com/p/minify/] is one approach.
Look at line 39 of the config file [http://code.google.com/p/minify/source/browse/trunk/min/config.php]. Here you will see where your minified cache sits outside of the web root. Now, I do not know if the source JS and CSS can sit in the same directory as the cache.
Not without a public facing proxy.
You will need to file_get_contents() or include them and then serve them to your page.
You can not just do ../../system and get above the DOCROOT.
They need to be processed by the browser, so they need to be accessible.
If you want to hinder people viewing your source in a human readable way, check out CSS minify and JS packer. These of course are only obfuscating the code. Anyone determined will be able to read your JavaScript and see what it does.
Why don't you want people to read your CSS or JavaScript?
I know what you mean twmulloy, it seems inconsistent to have 'view' related information in different places. However, consider that the JS and CSS files are resources that support the views, rather than parts of the view themselves.
That said, you can achieve what you want in a number of ways. One might be to write a controller that accepts requests for your JS/CSS assets and outputs a header and data from the relevant place (a view file, the database, anywhere in fact). However, this is inefficienty compared to just accepting the 'untidiness' of popping the files in a subfolder of the root level public_html. I, like many commentors above, feel this is the best solution for its speed and appropriateness; just having an 'assets' directory at the same level as the 'system' one, with images, css, js etc inside. You could use an alias or virtual folder to make things feel better for you...
However, there is a third way. There are libraries that do something JUST like what you want, with the added benefit of Minify (from the accepted answer) and compression, or whatever you fancy. The two libraries I know of are called AssetLibPro and Carabiner, and these allow you to specify an asset path (as you want), and then you load your JS and CSS files (with groups e.g. screen, print if needed). They then serve up all related CSS/JS etc as one file; compressed, minified, cached... whatever you need.
Carabiner: http://codeigniter.com/wiki/Carabiner/
AssetLibPro: http://codeigniter.com/forums/viewthread/78931/

How to load new images, when changed in a web application

How to push new images in a web application, so that the cached is not taken?
When I am having a new JS or CSS file, it's easy.
Because they are in smarty templates, and I am having a version number in the URL (like a.js?v=9).
Now, the problem with images are: -
They are referred from the CSS files, and I can not have a version variable there.
So, how do you do it?
In the middle between cleanest and easiest way, I would :
In the CSS, point to images with URLs containing a distinct marker ; like "image.png?VERSION_NUMBER" (literaly)
this will allow the CSS file to be used while developping
To avoid any problem with cache, I would configure Apache (on the development machine) to indicate files should not be cached by the browser
I would use some kind of "build process", that would replace this VERSION_NUMBER marker by the real version number in every CSS file (and possibly, JS, PHP, HTML, ... )
This would create modified files, containing the right version number
Those files would be the ones deployed to the webserver
Ideally, the VERSION_NUMBER could be the SVN revision of each file ; this way, only files really modified would have to be modified ; but also harder : for each file (each URL in the CSS file !), you have to determine it's revision number before replacing the marker !
If some browser don't cache images/js/css because of the query string, the marker could be included in the files' names.
And now that you have a "build process", you can also use it to make some other manipulations, like minifying JS and CSS files, for instance
As a side note : yes, creating and testing the build process / script takes some time ; it might be easier to server CSS files through PHP, using a variable in those to indicate the version number... But, for performances, serving CSS files (at least one per PHP page ; probably more !) wouldn't be that wise ; so, probably better to take a bit more time to write the build process...
You could manually change the CSS file at the same time that you change the image file, put a "?v=1" onto the end of the image url.
You could also configure your server to send CSS files through the PHP processor, so you could stick some PHP code in there to set the "?v=8" query string on the image url.
As part of my build process, I append a querystring to javascript includes and image URLs that has the file's LastModified date/time as a long. This allows caching to work when it should, and automates something that is easy for the developer to forget.

Categories